开发者

Select an array of elements in jQuery

I need to wrap up an array of elements into a jQuery object as if they were selected so that I can call various jQuery actions on them.

I'm looking for a function like foo below that accepts an array of elements and returns a jQuery object with them in it.

var elements = [element1, element2, element3];

$(foo(elements)).click(function() {
    开发者_如何学Go...
});

Can someone shed some light on this?

Thanks much.


Just do

$(elements).click( function(){ ... });

if your elements are actual references to the DOM

demo: http://jsfiddle.net/gaby/dVKEP/


Use jQuery.each

Example:

$.each(elements, function(index, element) { 
    $(element).doStuff();
});


Use each to iterate over both objects and arrays

var elements = ['element1', 'element1', 'elements3'];
$.each(elements, function(index, value) {
    alert(index + ': ' + value);
});

Check working example at http://jsfiddle.net/LpZue/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜