获取和修改text node

text node 是指 <h2><span>In span</span>Text Node </h2>

 

JQuery没有直接对文本节点进行操作的接口。

 

获取:采用节点类型过滤。

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

 

修改:采用移除再添加的方法。

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

$("h2").append("New Text");

你可能感兴趣的:(node)