jQuery: Target all except ___?

前端 未结 4 1797
终归单人心
终归单人心 2020-12-06 09:53

I\'m looking for a way to select all elements on a page, except those with a specified DOM location.. Here\'s an example of what I\'d like to do:

jQuery(\'*\         


        
相关标签:
4条回答
  • 2020-12-06 10:32

    Another way, if you already have selectors for both:

    $('.foo').not('.ignore').bind(...);
    

    Also, more filters.

    0 讨论(0)
  • 2020-12-06 10:39

    You want to use the :not() selector:

    jQuery(":not(.ignore)").bind("click", function(e) { ... });
    
    0 讨论(0)
  • 2020-12-06 10:40

    jQuery not-selector to the rescue!

    $('*:not(.ignore)').bind('click', function(e) { ... });
    
    0 讨论(0)
  • 2020-12-06 10:43

    On the other hand, doing something to every element on a page simultaneous is nasty. There's a better way. I would recommend binding to the body then ignoring clicks on some elements:

    $(document.body).click(function(e){
        if($target.closest('.ignore').length) return true;
        ...
    });
    

    …Or using jQuery 1.3's .live(), which does this for you:

     $(":not(.ignore)").live(function(e){
        ...
    });
    
    0 讨论(0)
提交回复
热议问题