Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想用 html<br />标签替换字符串中的空格,最好的方法是什么?目前我有这个代码用“-”替换空格。
<br />
$(".msn").each(function() { $(this).text($(this).text().replace(/ /g, '-')); });
$(".msn").html(function(i, oldHTML) { return oldHTML.replace(/ /g, '<br/>'); });
尝试使用.html()而不是.text():
.html()
.text()
$(".msn").each(function() { $(this).html($(this).html().replace(/ /g, '<br />')); });