简体   繁体   中英

How to get multiple nodes with jquery

While for a single element we can use jQuery('#elem').get(0), is there a way we can get the node elements for multiple selection:

like: jQuery('.elements').getAll(0) ?

I'm looking for a solution, if exists, without a loop iteration.

No single method to do that exists. The only other option you have is to get all of the matched nodes, by not passing an argument to .get() :

var all = jQuery('.elements').get();

However, you could reduce the matched set down to the elements you require before calling .get() to acheive the same thing. You can use .slice() to return a subset of the matched elements between two indicies:

var all = jQuery('.elements').slice(1, 3).get();

Other than that, your best bet will probably be to use some form of iteration (eg .filter() ) to return the elements at specific indicies, and the use .get() again:

var all = jQuery('.elements').filter(function (i) {
    return i === 2 || i === 5 || i === 9;
}).get();

Simply use the selection...

For single class

$('.elements').get() // It will return all the DOM elements have class="elements <other_classes>"

For multiple classes

$('.elements, .nav, .address').get() // It will get all the DOM elements has either of these classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM