繁体   English   中英

html标签插入换行符

[英]html label inserting newline

我正在尝试使用jQuery动态地将一些文本设置为标签。 但我无法得到<br>\\n来渲染并给我新的线条。 这一切都显示在同一条线上并且包装。

这是我的代码On JsFiddle

我的HTML:

<label id="myLabel" />

我的Javascript:

$("#myLabel").text("This is my first line \n This should be my second line.");

text()将转义任何html代码,因此改为使用html()

$("#myLabel").html("This is my first line <br /> This should be my second line.");

问题是.text()会忽略你的html。 你应该使用.html() ,大豆你可以把你想要的任何html放在一个元素中。 所以你有:

$('#myLabel').html('This is my first line <br/> This should be my second line.');

希望这可以帮助。 干杯

试试这个:

$('#myLabel')
    .html('this is my first line<br/>This should be my second line.');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM