在textarea的div中设置html时保留换行符-jQuery [英] Preserve newlines when setting html in a div from a textarea - Jquery

查看:325
本文介绍了在textarea的div中设置html时保留换行符-jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个小html-:

I got this little html -:

<div id="viewOne"></div>
<textarea id="viewTwo"></textarea>
<button id="copyToDiv" value="Copy to Div"></button>

这是我的Jquery片段-:

This is my Jquery snippet-:

$("#copyToDiv").on("click",function(){ $("#viewOne").html( $("#viewTwo").val() ) });

但是它从textarea的val中剥离了new line characters,我得到的是带有新行的字符串.设置div的html时如何保留换行符. 非常感谢您的帮助:)

But it strips of new line characters from textarea's val and what i get is string with new lines stripped off. How do I preserve newlines when setting the html of div. Thanks a lot for help :)

推荐答案

新行将保留,但不会转换为HTML新行(<BR/>),因此将被忽略.

The new lines are preserved, but not converted to HTML new lines (<BR/>), so they are ignored.

您可以使用.replace将它们转换为<BR/>:

You can convert them to <BR/> with .replace:

$("#copyToDiv").on("click",function(){ $("#viewOne").html( $("#viewTwo").val().replace("\n","<br/>") ) });

这篇关于在textarea的div中设置html时保留换行符-jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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