“All but not” jQuery selector

后端 未结 7 1375
谎友^
谎友^ 2020-12-05 16:44

I can select (using jQuery) all the divs in a HTML markup as follows:

$(\'div\')

But I want to exclude a particular div (say h

7条回答
  •  鱼传尺愫
    2020-12-05 17:27

    Simple:

    $('div').not('#myid');
    

    Using .not() will remove elements matched by the selector given to it from the set returned by $('div').

    You can also use the :not() selector:

    $('div:not(#myid)');
    

    Both selectors do the same thing, however :not() is faster, presumably because jQuery's selector engine Sizzle can optimise it into a native .querySelectorAll() call.

提交回复
热议问题