html标签插入换行符

时间:2011-05-06 21:01:44

标签: javascript jquery html

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

这是我的代码On JsFiddle

我的HTML:

<label id="myLabel" />

我的Javascript:

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

3 个答案:

答案 0 :(得分:22)

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

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

答案 1 :(得分:3)

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

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

希望这会有所帮助。干杯

答案 2 :(得分:1)

请改为尝试:

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