清除div的文本,但保持子节点使用jquery [英] Clear text of div , but keep child nodes using jquery

查看:431
本文介绍了清除div的文本,但保持子节点使用jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

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>

在上面的例子中,我只想清除文本 父母 div parent ),保留子节点( child 1 child2 )完好无损。如何使用jQuery?

In the above example, I want to clear only text "text of parent" of parent div (parent), keeping child nodes(child 1, child2) intact. How can I do this using jQuery?

推荐答案

尝试

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

http://jsfiddle.net/eUW47/1

这篇关于清除div的文本,但保持子节点使用jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆