当不存在内部元素时删除类 [英] Remove class when no inner elements exist

查看:57
本文介绍了当不存在内部元素时删除类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程:

<div class='prod-item'>
</div>

我在DOM中可能有此类的多个版本.是否可以遍历每个元素,并删除没有内部HTML元素的元素?

I may have multiple versions of this class within the DOM. Is it possible to iterate through each, and remove those with no inner HTML elements?

例如,可以保留:

<div class='prod-item'>
<p>Pussy</p>
</div>

这应该去:

<div class='prod-item'>
</div>

我具有以下不完整的功能,但不确定如何检查内部元素是否存在:

I have the following incomplete function, but I'm unsure on how to check if inner elements exist:

$('li[class="prod-item"]').each(function() {
    if(){
     this.removeAttribute('class');
   }
});

推荐答案

$(".prod-item").each(function() {
   if($(this).children().length == 0) {
     $(this).remove();
   }
});

这篇关于当不存在内部元素时删除类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆