这个问题让我感到困惑。我想要从一个
.html()
而不是.text()
来实现您想要的代码功能:$(".option").each(function(index, value) {
var oldString = $(this).html();
var newString = oldString.replace("+", "");
console.log(oldString, newString);
$(this).html(newString);
});
var node = document.getElementById("example").childNodes[2];
node.nodeValue = node.nodeValue.replace("+", "");
这里有一个简单的演示。
在IT技术中,尽量使用纯JS代替jQuery,因为纯JS往往比jQuery更快。
$(function () {
$('label.option').each(function () {
var label = $(this);
var input = $('input.form-radio', this);
var text = label.text();
// Clean up the label contents.
label.empty();
// Replace the char occurrences.
text = text.replace(/\+/g, "");
// Append both the input and the modified text.
label.append(input).append(text);
});
});
detach
。将下一行设置文本为空就足够了。 - James Montagne
Abc + 123 + 456
,应该变成Abc 123 + 456
,但是这段代码返回的结果是Abc 123 456
。 - Addo Solutions