如何检测浏览器的“换行”表示是否正确有两个角色?
据我所知,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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下似乎在最新的 Opera 和 IE 7+ 中有效(即返回所需的
true
)。然而,这在旧版本中未经测试,我怀疑这是不必要的。请参阅我对该问题的评论。更新:由于下面@Umbrella 的评论而得到改进。
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.
与我的其他答案类似,但我想到不需要创建专用的文本区域,您可以使用主题文本区域来确定这一点,只要其中有任何换行符即可。如果没有,不知道不会影响你的范围。
然后测试它
这将返回/警告文本区域中换行符的宽度。
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.
and then test it
This will return/alert the width of a newline in your textarea.
我不知道应该如何检查这一点。我希望有人这样做,但万一没有人这样做,我有一个关于如何测试的想法。添加(硬编码)一个包含换行符的已知值的文本区域,然后检查其长度。
确保第二行(包含“bar”)没有缩进/前导空格/制表符,然后对其进行测试。
当然,您会希望放弃警报并将其保存在 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.
Make sure the second line (containing 'bar') has no indentation/leading whitespace/tabs, and then test it
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.