我想從第二個孩子中洗掉逗號(,)。在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
uj5u.com熱心網友回復:
您必須將值設定為 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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/358720.html
標籤:查询
上一篇:JSRegex-非捕獲可選引號
