jQuery wrap every X elements in div

后端 未结 4 1027
栀梦
栀梦 2020-12-18 03:17

I have a list of elements (divs) preseded by a H3 tag

<
4条回答
  •  萌比男神i
    2020-12-18 03:45

    You could do something like this:

    (function(c, h, s, $g, n) {
        $(c).find([h,s].join()).each(function() {
            if ($(this).filter(h).length || $g.find(s).length == n) {
                $g = $g.clone().empty().insertAfter(this);
            }
            $g.append($(this).not(h));
        });
    })(document, 'h3', '.item', $('
    '), 3);

    If your elements are contained in a specific container, then pass the container's selector (eg. "#myContainer") instead of document.

    DEMO

提交回复
热议问题