如何使用jQuery或javascript删除文本

时间:2014-02-06 03:34:53

标签: javascript jquery

我无法想出一种语法来删除以黄色突出显示的文本。当我尝试访问主面板时,PicPanel div也会被清除 enter image description here

3 个答案:

答案 0 :(得分:3)

var parent = document.getElementById('ct100...');

parent.removeChild(parent.firstChild);

这是小提琴:http://jsfiddle.net/9B36K/


如果你想确保你只删除那个文本节点(如果它不存在则不会意外删除其他东西),请使用:

var parent = document.getElementById('ct100...');
var child  = parent.firstChild;

child.nodeType == 3 && parent.removeChild(child);

答案 1 :(得分:1)

这是一个文本节点,要删除它,您需要对其进行过滤:

HTML:

<div id="root">
    Some text to be removed.
    <div>Some div not to be removed</div>
</div>

JavaScript(jQuery)

$('#root').contents().filter(function() {
    return this.nodeType == 3; // this is a text node
}).remove();

请参阅JSFiddle

答案 2 :(得分:0)

举个例子:

<div id="parent">
 Parent
<div id="child">child</div>
</div>

尝试使用此代码段:

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