Javascript html标签插入换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5916967/
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
html label inserting newline
提问by capdragon
I'm trying to set some text to a label dynamically using jQuery. But i can't get the <br>
or \n
to render and give me new lines. It all shows up on the same line and wraps.
我正在尝试使用 jQuery 动态地将一些文本设置为标签。但我无法获得<br>
或\n
渲染并给我新的线条。它全部显示在同一行并换行。
here is my code On JsFiddle
这是我在 JsFiddle 上的代码
My html:
我的html:
<label id="myLabel" />
My Javascript:
我的Javascript:
$("#myLabel").text("This is my first line \n This should be my second line.");
回答by ariel
text()
will escape any html code, so instead use html()
text()
将转义任何 html 代码,所以改为使用 html()
$("#myLabel").html("This is my first line <br /> This should be my second line.");
回答by Edgar Villegas Alvarado
The problem is that .text()
will ignore your html. You should use .html()
, soy u can put whatever html you want inside an element. So you'd have:
问题是.text()
会忽略你的html。你应该使用.html()
, soy 你可以把你想要的任何 html 放在一个元素中。所以你会有:
$('#myLabel').html('This is my first line <br/> This should be my second line.');
Hope this helps. Cheers
希望这可以帮助。干杯
回答by Jacob
Try this instead:
试试这个:
$('#myLabel')
.html('this is my first line<br/>This should be my second line.');