jQuery:如何删除文本,但不删除子元素 [英] jQuery: how to remove the text but not the children elements
本文介绍了jQuery:如何删除文本,但不删除子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以仅使用jQuery删除节点中的文本,而不能删除子元素吗?
can I remove with jQuery only the text in a node but not the children elements ?
谢谢
推荐答案
这是我的主意:
function removeText(element){
var newElement = $('<' + element[0].nodeName + '/>');
for(i=0; i < item.attributes.length; i++) {
newElement.attr(item.attributes[i].name, item.attributes[i].value);
}
element.children().each(function(){
newElement.append(this);
});
element.replaceWith(newElement);
}
这篇关于jQuery:如何删除文本,但不删除子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文