我有一个文本输入和一个包含相同文本的 div。我正在尝试使 DIV 文本中的换行符与输入文本中的换行符相同。所以我用了 .replace(/\n/g
去做这个。替换成功识别文本区域中的换行符并能够添加 <br>
因此。但是 <br>
不做换行符,它是文本字符串的一部分?
如何制作 <br>
换行?
看例子:
<textarea id="ab">
Test
Line breaks
on blur
</textarea>
<div id="divTx"><div>
.on("blur", ".ab", function() {
$('#divTx').text($('#ab').val().replace(/\n/g, '<br />') );
});
DIV (.divTx) 输出:
Test<br> Line breaks<br>on blur<br> // <br> doesn't do line break
最佳答案
您需要使用 .html(htmlString)
Set the HTML contents of each element in the set of matched elements.
使用
$('#divTx').html($('#ab').val().replace(/\n/g, '<br />'))
关于javascript - 使用 replace() 添加中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30370048/