Turn array of jQuery elements into jQuery wrapped set of elements

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

    You can use the add method to copy the elements in a jQuery object to another. This will copy all elements from each of the jQuery objects in the array source into the jQuery object items:

    // Create an empty jQuery object
    var items = $([]);
    // Add the elements from each jQuery object to it
    $.each(source, function(){ items = items.add(this); });
    

    (Prior to version 1.3.2 the add method doesn't support adding a jQuery object, so you would need to use items.add(this.get()); instead.)

提交回复
热议问题