开发者

referencing DOM elements in jQuery

开发者 https://www.devze.com 2022-12-27 14:06 出处:网络
I would select multiple DOM elements using a typical code such as this: $(\'#ele1, #ele2, #ele3\').click(function () {});

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')});
})
0

精彩评论

暂无评论...
验证码 换一张
取 消