jQuery textarea 追加换行行为 [英] jQuery textarea append newline behavior

查看:141
本文介绍了jQuery textarea 追加换行行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 jQuery 将以换行符结尾的字符串附加到文本区域.然而,不同的换行符在 Firefox3.5 和 IE8 中表现出不同的行为,我似乎无法找到一种方法来使用对这两种浏览器都适用的东西.

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.

  • \n 适用于 FF 但不适用于 IE
  • <br/>\r\n 在 IE 中有效,但在 IE 中无效FF
  • 使用 <pre></pre> 标签也不走运
  • \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

我已经看到有关 IE innerHTML 问题的信息,但我不确定如何在 jQuery 中最好地解决这个问题.感谢您的帮助!

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!

推荐答案

不确定你是如何设置 textarea 内容的,但是如果你使用 jQuery val 方法,\n在 Firefox 和 IE(包括 IE8)中一致工作:

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):

var txt = $("textarea#idhere");
txt.val( txt.val() + "\nSomething here\n\nAgain");

使文本区域显示:

现有内容
和换行符(如果有的话).
这里有东西

Existing content
and linebreaks if any.
Something here

再来

您可以在此处查看适用于 FF 和 IE8 的演示:演示 |来源

You can see a demo here that works in FF and IE8: Demo | Source

这篇关于jQuery textarea 追加换行行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆