匹配元素没有特定元素

时间:2015-08-19 16:22:07

标签: jquery

我需要运行.myEm类的html块,如果.myEm没有任何<p>标签,则从该元素的第一个div中删除一个类。

这是我想出来的,但它仍然没有删除课程。我错过了什么?

$('.myEm').each(function() {
    $(this).not(':has(p)').find('.row:first-child').removeClass('myClass');
});

2 个答案:

答案 0 :(得分:1)

使用find()并查看length

$('.myEm').each(function() {
    if($(this).find('p').length == 0){
        $(this).find('.row:first-child').removeClass('myClass');
    }
});

答案 1 :(得分:0)

你可以在一次扫描中实际做到这一点;许多jQuery方法在内部使用.each()

$('.myEm:not(:has(p)) .row:first-child').removeClass('myClass');