Gir*_*shK 3 html javascript jquery dom
可能重复:
jQuery:如何删除文本但不删除子元素
<div id="parent" style="border:2px solid red; width:300px;height:200px">
text of parent
<div id="child1" style="border:1px solid green; width:200px;height:80px"></div>
<div id="child2" style="border:1px solid blue; width:200px;height:80px"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我想只清除"text of parent"parent div(parent)的文本,保持子节点(child 1,child2)不变.我怎么能用jQuery做到这一点?
尝试
$("#parent").contents().filter(function(){
return (this.nodeType == 3);
}).remove();
Run Code Online (Sandbox Code Playgroud)