“All but not” jQuery selector

后端 未结 7 1379
谎友^
谎友^ 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:20

    var els = toArray(document.getElementsByTagName("div"));
    els.splice(els.indexOf(document.getElementById("someId"), 1);
    

    You could just do it the old fashioned way. No need for jQuery with something so simple.

    Pro tips:

    A set of dom elements is just an array, so use your favourite toArray method on a NodeList.

    Adding elements to a set is just

    set.push.apply(set, arrOfElements);

    Removing an element from a set is

    set.splice(set.indexOf(el), 1)

    You can't easily remove multiple elements at once :(

提交回复
热议问题