使用 Jquery.text() 时 <p> 标签内的换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4418543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:15:37  来源:igfitidea点击:

line break inside <p> tags when using Jquery.text()

jqueryhtmljquery-ui

提问by kralco626

I am trying to do something like this:

我正在尝试做这样的事情:

<p> Line 1 <br/> Line 2 <br/> </p>

<p> Line 1 <br/> Line 2 <br/> </p>

This does not work, but is what i'm trying to do possible?

这行不通,但我想要做的可能吗?

Edit: more info

编辑:更多信息

i am executing this line of code: $("#textDialogBox").text("test<br />test2");

我正在执行这行代码: $("#textDialogBox").text("test<br />test2");

where #textDialogBoxis <p id = "textDialogBox">This is an animated dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

这里#textDialogBox<p id = "textDialogBox">This is an animated dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

But when I open the dialog box the
shows up as text, rather than a line break.

但是当我打开对话框时,它
显示为文本,而不是换行符。

回答by Shadow Wizard is Ear For You

Have this instead:

有这个:

$("#textDialogBox").html("test<br/>test2");

The text() method of jQuery will "destroy" any HTML given to it, and this includes <br />as well.

jQuery 的 text() 方法将“销毁”任何给它的 HTML,这也包括在内<br />