Br在使用Jquery.html()时无法在P内部工作

Br not working inside P when using Jquery.html()

本文关键字:内部 工作 Jquery html Br      更新时间:2023-09-26

我试图为<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"

我的代码出了什么问题?或者我该怎么做?

您也可以尝试以下操作:

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

试试这个

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