What JQuery selector excludes items with a parent that matches a given selector?

前端 未结 7 790
Happy的楠姐
Happy的楠姐 2020-12-02 16:39

I have

var $set = $(\'.foo,.bar\').filter(
    function() {return $(this).parents(\'.baz\').length < 1;});

as a way to select all the el

相关标签:
7条回答
  • 2020-12-02 16:51

    try this one:

    $('div').filter(function () {
      return ($(this).parent().not('.baz'));
    })
    
    0 讨论(0)
  • 2020-12-02 16:53

    var $li = jQuery('li', this).not('.dropdown-menu > li');

    I'm using Roots theme and they change the dropdowns from 'sub-menu' to '.dropdown-menu'

    0 讨论(0)
  • 2020-12-02 16:54
    $('.foo:not(.baz .foo),.bar:not(.baz .bar)')
    
    0 讨论(0)
  • 2020-12-02 16:54

    I couldn't get this to work:

    $(".children:not(#parent .children)");
    

    But I can get this to work:

    $(".children").not($("#parent .children"));
    

    Note: Not sure if this has something to do with the jQuery version I'm using 1.2.6

    0 讨论(0)
  • 2020-12-02 17:09

    Add a :not('.baz') to your top level selector.

    0 讨论(0)
  • 2020-12-02 17:11

    The selector chaos gives should work:

    $('.foo:not(.baz .foo),.bar:not(.baz .bar)')
    

    Just wanted to give you a tip about an extension to FireBug called FireFinder that you can use to test your css/jquery selectors.

    From the website: Firefinder is an extension to Firebug (in Firefox) and offers the functionality to, in a quick way, find HTML elements matching chosen CSS selector(s) or XPath expression. It allows you to instantly test your CSS selectors in the page while seeing the content at the same time, and matching elements will be highlighted.

    0 讨论(0)
提交回复
热议问题