1

Sometimes, people send messages from their computer, but how can I find out where the line break was added in their text?

For example, in this text below:

ABC<br>
123

there is an carriage return between C and 1

How can I detect this in JavaScript?

4

2 回答 2

1

您可以为 keydown 事件编写侦听器。比您将通过事件对象(事件侦听器参数)的 keyCode 属性(ENTER = 13)识别单击的按钮。

<html>
<head>
    <script type="text/javascript">
        <!--
            function onKeyPress(event) {
                    switch (event.keyCode) {
                    case 13:
                        alert("You have clicked ENTER");
                        break;
                    default:
                }
            }

            window.onload = function() {
                window.addEventListener('keydown', onKeyPress, false);
            }
        //-->
    </script>
</head>
<body></body>
</html>
于 2013-08-23T08:16:51.540 回答
0

换行符在 JavaScript 中编码为\n代表 unicode 字符U+0010。回车 ( U+0013) 是\r。您的字符串可以用 JavaScript 编码为:

"ABC\n123";

或者:

"ABC\r\n123";

您还可以使用例如\x10或来通过代码点引用任何字符\u0010

这篇文章很好读:每个软件开发人员绝对、肯定必须了解 Unicode 和字符集的绝对最低要求(没有借口!)

于 2013-08-23T08:16:49.743 回答