Turn array of jQuery elements into jQuery wrapped set of elements

前端 未结 11 1053
长发绾君心
长发绾君心 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条回答
  •  梦毁少年i
    2021-02-01 13:10

    If Array.prototype.reduce() is supported in your environment.

    var jQueryCollection = [$("a"), $("div")]
                              .reduce(function (masterCollection, collection) {
                                   return masterCollection.add(collection);
                              }, $());
    

    jsFiddle.

提交回复
热议问题