Can I $.wrap() around a collection of elements in an array?

后端 未结 3 1443
长情又很酷
长情又很酷 2021-02-09 00:07

Let\'s say I have a collection of \'items\' like so:

3条回答
  •  Happy的楠姐
    2021-02-09 00:53

    Use wrapAll instead of wrap.

    $(".group").wrapAll('
    ');

    Documentation of wrapAll can be found at - http://api.jquery.com/wrapAll/

    Other wrapping methods available can be found at - http://api.jquery.com/category/manipulation/dom-insertion-around/

    EDIT:

    For the complex case where there can be more than one groups, we can achieve it using wrapAll with a $.each as follows -

    var group = [];
            $(".item").each(
              function(i, item) {            
                if ($(item).hasClass("group")) {
                    group.push(item);
                }
                else {
                    $(group).wrapAll('
    '); group = []; } } );

提交回复
热议问题