如何检测浏览器的“换行”表示是否正确有两个角色?

发布于 2024-12-27 18:57:37 字数 253 浏览 1 评论 0原文

据我所知,Opera 将 \n\r 视为文本区域内的新行。还有一些IE版本。 如果我在文本区域内按 Enter 键,我将得到带有 \n\r 的新行。

在其他浏览器中,新行只是 \n

我如何在我的脚本中检测到这一点?

我可以使用 window.opera 来查明浏览器是否是 Opera,但人们说我应该尝试检测浏览器功能而不是实际的浏览器。

I understand that Opera considers \n\r a new line inside textareas. And some IE versios.
If I press Enter inside a textarea, I'll get new lines with \n\r

In other browsers the new line is just \n.

How can I detect this in my script?

I could use window.opera to find out if the browser is Opera, but people say I should try to detect browser features instead of the actual browser.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剩余の解释 2025-01-03 18:57:37

以下似乎在最新的 Opera 和 IE 7+ 中有效(即返回所需的 true)。然而,这在旧版本中未经测试,我怀疑这是不必要的。请参阅我对该问题的评论。

更新:由于下面@Umbrella 的评论而得到改进。

var textarea = document.createElement("textarea");
textarea.value = "\n";
var twoCharLineBreaks = (textarea.value == "\r\n");
alert(twoCharLineBreaks);

The following seems to work (i.e. returns the desired true) in the latest Opera and IE 7+. However, this is untested in older versions and I suspect is unnecessary. See my comments on the question.

Update: Improved thanks to a comment from @Umbrella below.

var textarea = document.createElement("textarea");
textarea.value = "\n";
var twoCharLineBreaks = (textarea.value == "\r\n");
alert(twoCharLineBreaks);
生寂 2025-01-03 18:57:37

与我的其他答案类似,但我想到不需要创建专用的文本区域,您可以使用主题文本区域来确定这一点,只要其中有任何换行符即可。如果没有,不知道不会影响你的范围。

<textarea id="yourEditor">foo
bar
bing
baz</textarea>

然后测试它

<script type="text/javascript">
function newlineSize() {
    var val = document.getElementById('yourEditor').value;
    var len = 0;
    if (1 < val.split("\r").length) {
        len++;
    }
    if (1 < val.split("\n").length) {
        len++;
    }
    return len;
}
</script>

<input type="button" onclick="alert(newlineSize());" value="test">

这将返回/警告文本区域中换行符的宽度。

Similar to my other answer, but it occurred to me that the creation of a dedicated textarea is not required, you can determine this with the subject textarea, so long as it has any newlines in it. if it doesn't, not knowing won't affect your range.

<textarea id="yourEditor">foo
bar
bing
baz</textarea>

and then test it

<script type="text/javascript">
function newlineSize() {
    var val = document.getElementById('yourEditor').value;
    var len = 0;
    if (1 < val.split("\r").length) {
        len++;
    }
    if (1 < val.split("\n").length) {
        len++;
    }
    return len;
}
</script>

<input type="button" onclick="alert(newlineSize());" value="test">

This will return/alert the width of a newline in your textarea.

悲欢浪云 2025-01-03 18:57:37

我不知道应该如何检查这一点。我希望有人这样做,但万一没有人这样做,我有一个关于如何测试的想法。添加(硬编码)一个包含换行符的已知值的文本区域,然后检查其长度。

<textarea style="visibility:hidden" id="newlineTest">foo
bar</textarea>

确保第二行(包含“bar”)没有缩进/前导空格/制表符,然后对其进行测试。

<script type="text/javascript">
function newlineSize() {
    var len = document.getElementById('newlineTest').value.length;
    alert(len);
    if (8 == len) {
        return 2;
    } else if (7 ==len) {
        return 1;
    } else {
        return null; //unknown condition
    }
}
</script>

<input type="button" onclick="alert(newlineSize());" value="test">

当然,您会希望放弃警报并将其保存在 var 中。在 Chrome、Firefox、Safari(所有 mac)中测试,返回 1。IE8(XP) 返回 2。

I don't know how one should check for that. I hope someone does, but in case no one does, I have an idea about how you can test for that. Add (hard code) a textarea with a known value containing a newline, then check its length.

<textarea style="visibility:hidden" id="newlineTest">foo
bar</textarea>

Make sure the second line (containing 'bar') has no indentation/leading whitespace/tabs, and then test it

<script type="text/javascript">
function newlineSize() {
    var len = document.getElementById('newlineTest').value.length;
    alert(len);
    if (8 == len) {
        return 2;
    } else if (7 ==len) {
        return 1;
    } else {
        return null; //unknown condition
    }
}
</script>

<input type="button" onclick="alert(newlineSize());" value="test">

Naturally you'd want to ditch the alerts and save it in a var. Tested in Chrome, Firefox, Safari (all mac), returned 1. IE8(XP) returned 2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文