我不知道如何称呼它,但我称之为半内部文本。
例子
<div id='myDiv'>
This is the semi-inner text
<div id='other'></div>
This is the semi-inner text
<div>
我想使用jquery删除/替换那些
这是半内部文本
。我该怎么做?
$('#myDiv').contents().filter(function() {
return this.nodeType == 3
}).each(function() {
this.data = this.data.replace(/This is the semi-inner text/g, 'swapped');
});
如果您想检查所有后代节点,请在检测到元素节点(this.nodeType == 1
)时使函数进行递归。
$('#myDiv')
.contents()
.filter(function() {
return this.nodeType == 3;
})
.each(function() {
$(this).remove();
});
$('#myDiv').contents().filter(function() {
return this.nodeType == 3;
}).remove();
$("#other").html("your code");
并且用户将您的代码包装在带有ID的span标签中
<div id='myDiv'>
<span id="myInnerText">This is the semi-inner text</span>
<div id='other'></div>
This is the semi-inner text
<div>
然后像这样替换它:
$("#myInnerText").text("replacement text");
你也可以像这样做:
target_div = $("#myDiv")
reg_exp = /This is the semi-inner text/g;
new_text = "some other text"
target_div.html(
target_div.html()
.replace(
new RegExp(reg_exp),
new_text)
);
});
<span class =“semiTxt”> </ span>
中,并替换其中的文本 - Bongs