Turn array of jQuery elements into jQuery wrapped set of elements

前端 未结 11 1156
长发绾君心
长发绾君心 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:05

    you could do something like this:

    var $temp = $();
    $.each([$("li"), $("li"), $("li")], function(){
        $temp.push(this[0]);
    })
    

    $temp all your elements in one jQuery selector

    But i am curious what brings you to this situation having an array of different jQuery elements. You know that you can select different elements using a comma? like $("li, ul > li:eq(0), div")

    edit as Guffa pointed out, this only adds the first element of each section. .add() is a better choice then .push() here.

提交回复
热议问题