2011-05-06 155 views
7

我試圖設置一些文本動態使用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."); 

回答

20

text()將難逃任何HTML代碼,所以改用html()

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

試試這個:

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

的問題是,.text()會忽略你的HTML。你應該使用.html(),大豆你可以把任何HTML你想要的元素。所以你會有:

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

希望這會有所幫助。歡呼聲