我有一个标题与一些其他元素在里面。例如:
<h3 id="header" >
<span ...></span>
TITLE
<img ...>
</h3>
我想用其他文本替换"TITLE"
。我就是这么做的:
$("#header").contents().filter(function(){return this.nodeType == 3;}).text("some other text");
你可以这样做:
$('#header').contents().filter(function () {
return this.nodeType === 3 && $.trim(this.nodeValue).length;
}).replaceWith('some other text');
<<p> 小提琴演示/strong> 你可以在文本周围换行一个类,然后用
代替 $("#header .title-class").replaceWith( "New heading" );
我不知道,为什么上面的解决方案在JSFiddle中工作,但不适合我的代码。然而,我找到了另一个对我很有效的解决方案:
$("#header").contents().filter(function(){return this.nodeType == 3;}).remove();
$("header").append('some other text');