如何删除孩子的特殊字符



我想从第二个子项中删除逗号(,(。在console.log()中完全删除逗号,但在标签中逗号未删除

下面是我的代码:

$(document).ready(function() {
var child = $("span").children()[1];
$(child).html().replace(/,/g , ''); // in second child comma is not removed
console.log($(child).html().replace(/,/g , '')); // remove comma perfectly
});
li { 
display: inline-block; 
}
<!DOCTYPE html>
<html>
<head>
<title>Try jQuery Online</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<span>
<li>One, </li>
<li>Two,</li>
</span>
</body>
</html>

预期输出:

One, Two

必须将值设置为DOM元素

$(child).html($(child).html().replace(/,/g , ''));

$(document).ready(function() {
var child = $("span").children()[1];
$(child).html($(child).html().replace(/,/g , '')); // You must set the value to the DOM
console.log($(child).html().replace(/,/g , '')); // remove comma perfectly
});
li { 
display: inline-block; 
}
<!DOCTYPE html>
<html>
<head>
<title>Try jQuery Online</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<span>
<li>One, </li>
<li>Two,</li>
</span>
</body>
</html>

最新更新