如何只提取包含没有文字的属性的标签?
来源:
<div style='some style' id='some id'>
some text
</div>
<a href='some url'>some link</a>
<img src='some url' />
结果:
"<div style='some style' id='some id'> </div> <a href='some url'> </a> <img src='some url' />"
答案 0 :(得分:2)
试试这个:
sample = $("<div><div style='some style' id='some id'>some text</div><a href='some url'>some link</a><img src='some url' /></div>");
output = removeText(sample);
function removeText(node){
node = $(node);
$.each( node.contents(), function(){
if(this.nodeType == 3) {
this.parentNode.removeChild(this);
}else {
removeText(this);
}
});
return node;
}
输出:
<div><div style="some style" id="some id"></div><a href="some url"></a><img src="some url"></div>
答案 1 :(得分:0)