我无法从字符串中删除空格。首先,我将div转换为text();
以删除标签(有效),然后尝试删除 
字符串的“ ”部分,但它不起作用。任何想法我在做什么错。谢谢。
newStr = $('#myDiv').text();
newStr = newStr.replace(/ /g,'');
$('#myText').val(newStr);
<html>
<div id = "myDiv"><p>remove space</p></div>
<input type = "text" id = "myText" />
</html>
使用该text
函数时,您不会得到HTML而是文本:
实体已更改为空格。
var str = " a b ", // bunch of NBSPs
newStr = str.replace(/\s/g,'');
console.log(newStr)
如果只想替换空格,
请在转换为文本之前进行替换:
newStr = $($('#myDiv').html().replace(/ /g,'')).text();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句