将jQuery元素数组转换为jQuery包装的元素集 [英] Turn array of jQuery elements into jQuery wrapped set of elements
问题描述
是否有任何将[$(div), $(span), $(li)]
转换为$(div, span, li)
的优雅方法?
Is there any elegant way of turning [$(div), $(span), $(li)]
into $(div, span, li)
?
我需要的是一组由jQuery包装的元素,而不是一组jQuery元素.我想用尽可能少的代码行做到这一点,并且循环最少(如果有的话).
What I need is a jQuery-wrapped set of elements instead of an array of jQuery elements. I would like to do this in as few lines of code as possible, and with minimal (if any) looping.
编辑:对于那些对此问题感到困惑的人,可以使用console.log在已已选择的元素数组上从firebug复制并粘贴此代码.
For those of you confused by this question, this code is copied and pasted from firebug using console.log on an array of elements that have already been selected.
推荐答案
jQuery的map()
函数非常适合重塑数组和/或jQuery集合.
jQuery's map()
function is perfect for reshaping arrays and/or jQuery collections.
因此,给出了一个像这样的数组集:
So, given an array set like so:
var arrayOfJQ_Objects = [$("div"), $("span"), $("li")];
只需一行代码(在jsFiddle上查看它):
This one line of code is all you need (See it in action at jsFiddle):
$(arrayOfJQ_Objects).map (function () {return this.toArray(); } );
此控制台在Firebug中显示:
Resulting in this console display in Firebug:
jQuery(div, span, li)
同样, jQuery的.toArray()
函数.
Reference, also, jQuery's .toArray()
function.
这篇关于将jQuery元素数组转换为jQuery包装的元素集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!