How can I select an element which does not contain a certain child element?

前端 未结 8 1122
抹茶落季
抹茶落季 2020-12-09 09:03

How can I apply jQuery

相关标签:
8条回答
  • 2020-12-09 09:22
    $('.test:not(:has(.example))')
    

    -or-

    $('.test').not(':has(.example)')
    
    0 讨论(0)
  • 2020-12-09 09:27

    Possibly

    $('.test').filter(function() { return !$(this).children('.example').length; });
    

    This filters out any elements that have any child that matches .example. If you want to filter based on descendants (not just children) that you can substitute .find for .children.

    0 讨论(0)
  • 2020-12-09 09:29
    $(':not(.test:has(.example))').css('color', 'red');​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
    

    http://jsfiddle.net/9fkz7y1g/

    0 讨论(0)
  • 2020-12-09 09:29
    if (!$('#yourDiv').children().hasClass("className")) {
        //i.e. yourDivID' has no any children whose class name =>'className'
    }
    
    0 讨论(0)
  • 2020-12-09 09:33

    jQuery contains():

    jQuery.contains(document.documentElement, document.body); // true
    jQuery.contains(document.body, document.documentElement); // false
    
    0 讨论(0)
  • 2020-12-09 09:35

    This problem seems ready-made for the filter function where you find all the .test objects and then when filtering retain only the ones that don't have .example in them:

    $(".test").filter(function() {
        return($(this).find(".example").length == 0);
    });
    
    0 讨论(0)
提交回复
热议问题