Use*_*678 15 javascript jquery text newline
我有以下代码:
var stringDisplay = "Hello\nWorld";
$("#meaning").text(stringDisplay);
它显示\n而不是换行符.
输出显示为Hello\nWorld.
我<br>也用tag来代替\n,但它仍然没有用.
Ble*_*der 29
您必须使用两者.html()并替换换行符:
var escaped = $('<div>').text(stringDisplay).text();
$('#meaning').html(escaped.replace(/\n/g, '<br />'));
另一种方法是设置元素的样式:
white-space: pre-wrap;
小智 9
这个怎么样
$('meaning').html('line1<br>line2');