Turn array of jQuery elements into jQuery wrapped set of elements

前端 未结 11 1052
长发绾君心
长发绾君心 2021-02-01 12:39

Is there any elegant way of turning [$(div), $(span), $(li)] into $(div, span, li)?

What I need is a jQuery-wrapped set of elements instead of

11条回答
  •  被撕碎了的回忆
    2021-02-01 13:25

    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")];
    


    This one line of code is all you need (See it in action at jsFiddle):

    $(arrayOfJQ_Objects).map (function () {return this.toArray(); } );
    

    Resulting in this console display in Firebug:

    jQuery(div, span, li)
    


    Reference, also, jQuery's .toArray() function.

提交回复
热议问题