这个问题已经在这里有了答案:




已关闭8年。





<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>

在上面的示例中,我只想清除父"text of parent"(div)的文本parent,保持子节点(child 1child2)完整。如何使用jQuery完成此操作?

最佳答案

尝试

$("#parent").contents().filter(function(){
    return (this.nodeType == 3);
}).remove();

http://jsfiddle.net/eUW47/1

09-30 19:18
查看更多