使用Jquery.html()时,在P内部不工作

时间:2013-08-22 21:46:14

标签: javascript jquery html

我正在尝试为<p>元素分配大量文本,其中包含一些<br />标记,因为它是html。我正在使用JQuery的.html()方法,但它不会显示换行符。

我的代码:

var text = "Hello, this is a pretty <br/> large text with some <br/> line breaks inside"
$('.container').append("<p class='myPClass'><p>");
$('.myPClass').html(text);

它确实将文本添加为​​“myPClass”html,但它完全忽略了<br/>标记。 我得到的结果是:

<p class='myPClass'>Hello, this is a pretty large text with some line breaks inside</p>

所以它看起来像:

"Hello, this is a pretty large text with some line breaks inside"

我想要的结果是什么:

<p class='myPClass'>Hello, this is a pretty <br/> large text with some <br/> line breaks inside</p>

所以它看起来像:

"Hello, this is a pretty
large text with some
line breaks inside"

我的代码出了什么问题?或者我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

您还可以尝试以下操作:

var text = "Hello, this is a pretty" + "<br/>" + "large text with some" + "<br/>" + "line   breaks inside"
$('.container').append("<p class='myPClass'><p>");
$('.myPClass').html(text);

答案 1 :(得分:1)

试试这个

var text = "Hello, this is a pretty <br/> large text with some <br/> line breaks inside"
$('.container').append("<p class='myPClass'></p>");
$('.myPClass').html(text);