jQuery textarea 追加换行行为
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2159622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery textarea append newline behavior
提问by Suan
I'm trying to append a strings which end in newlines to a textarea using jQuery. However, different newline tokens show different behavior in Firefox3.5 and IE8, and I can't seem to find a way to use something that works for both browsers.
我正在尝试使用 jQuery 将以换行符结尾的字符串附加到文本区域。然而,不同的换行符在 Firefox3.5 和 IE8 中表现出不同的行为,我似乎无法找到一种方法来使用对两种浏览器都适用的东西。
\n
works in FF but not in IE<br/>
and\r\n
work in IE but not in FF- No luck using
<pre></pre>
tags either
\n
适用于 FF 但不适用于 IE<br/>
并\r\n
在 IE 中工作但不在 FF 中工作- 使用
<pre></pre>
标签也不走运
I've seen info on the IE innerHTML issue but I'm not exactly sure how to best approach this problem in jQuery. Thanks for any help!
我已经看到有关 IE innerHTML 问题的信息,但我不确定如何最好地在 jQuery 中解决这个问题。谢谢你的帮助!
回答by Doug Neiner
Not sure how you are setting the textarea content, but if you use the jQuery val
method, \n
works consistently in Firefox and IE (Including IE8):
不确定您如何设置 textarea 内容,但如果您使用 jQueryval
方法,则\n
在 Firefox 和 IE(包括 IE8)中一致工作:
var txt = $("textarea#idhere");
txt.val( txt.val() + "\nSomething here\n\nAgain");
Causes the textarea to display:
使 textarea 显示:
Existing content
and linebreaks if any.
Something hereAgain
现有内容
和换行符(如果有)。
这里有东西再次
You can see a demo here that works in FF and IE8: Demo| Source
回答by John Boker
Does pressing the [enter] key cause a new line in IE8 for you? You may have to do what the answer to this question suggests: enter does not work in textarea in Internet Explorer 8
按 [enter] 键是否会为您在 IE8 中换行?您可能必须按照此问题的答案进行操作:在 Internet Explorer 8 中的 textarea 中输入不起作用