I would select multiple DOM elements using a typical code such as this:
$('#ele1, #ele2, #ele3').click(function () {});
If I have va开发者_如何学编程riables of each DOM element, e.g.
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]
)
It's like that:
$(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')});
})
精彩评论