简体   繁体   中英

referencing DOM elements in jQuery

I would select multiple DOM elements using a typical code such as this:

$('#ele1, #ele2, #ele3').click(function () {});

If I have variables of each DOM element, eg

var domEle1 = $('#ele1');
var domEle2 = $('#ele2');
var domEle3 = $('#ele3');

How can I select them all at once? Instead of individually...

domEle1.click(function () {});

Thanks!

jQuery(
    [domEle1, domEle2, domEle3]
    )

就像那样:

$(domEle1).add(domEle2).add(domEle3).hide(); //for example

You can use an array for this:

$( [ '#ele1', '#ele2', '#ele3' ] ).click( function() {
   //...
} );
$([domEle1, domEle2, domEle3]).each( function() {
    $(this).click(function(){alert('hello')});
})

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