3

我正在尝试为<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"

我的代码有什么问题?或者我怎么能做到这一点?

4

2 回答 2

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);
于 2014-03-11T05:12:00.867 回答
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);
于 2014-03-11T05:37:57.150 回答