我試圖設置一些文本動態使用jQuery標籤。但我不能讓<br>
或\n
呈現並給我新的線條。這一切都顯示在同一行,幷包裝。html標籤插入換行
這裏是我的代碼On JsFiddle
我的HTML:
<label id="myLabel" />
我的javascript:
$("#myLabel").text("This is my first line \n This should be my second line.");
我試圖設置一些文本動態使用jQuery標籤。但我不能讓<br>
或\n
呈現並給我新的線條。這一切都顯示在同一行,幷包裝。html標籤插入換行
這裏是我的代碼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.");
試試這個:
$('#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.');
希望這會有所幫助。歡呼聲