使用jQuery删除HTML文档中的空标签 [英] Remove empty tags in a HTML doc with jQuery
本文介绍了使用jQuery删除HTML文档中的空标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用jquery删除空的p标签.
I'm trying to remove empty p tags using jquery.
我写了以下内容,只是似乎行不通.
I've written the following only it doesn't seem to work.
$( 'p:empty' ).remove();
这是现场演示...
推荐答案
这是因为您的元素不为空.
That's because your element isn't empty.
执行此操作:
$('p').filter(function(){return $(this).text().trim().length==0}).remove();
如wirey所述,要使其在IE8上正常运行,您应该这样做:
As noted by wirey, to make it work on IE8, you should do this :
$('p').filter(function(){return $.trim($(this).text()).length==0}).remove();
这篇关于使用jQuery删除HTML文档中的空标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文