简体   繁体   English

jQuery用html替换字符串中的空间 <br /> 标签

[英]Jquery Replacing space in string with html <br /> tag

I want to replace spaces in a string with a html <br /> tag, whats the best way of doing this?. 我想用html <br />标记替换字符串中的空格,最好的方法是什么? Currently I have this code which replaces the spaces with a "-". 目前,我有这段代码用“-”代替空格。

$(".msn").each(function() {
    $(this).text($(this).text().replace(/ /g, '-'));
});
$(".msn").html(function(i, oldHTML) {
    return oldHTML.replace(/ /g, '<br/>');
});

Try using .html() instead of .text() : 尝试使用.html()而不是.text()

$(".msn").each(function() {
    $(this).html($(this).html().replace(/ /g, '<br />'));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM