jQuery wrap every X elements in div

后端 未结 4 1796
走了就别回头了
走了就别回头了 2020-12-18 03:02

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

<
相关标签:
4条回答
  • 2020-12-18 03:28

    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', $('<div class="itemGroup"/>'), 3);
    

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

    DEMO

    0 讨论(0)
  • 2020-12-18 03:39

    There is a plugin here that I use when I want to wrap, just because it's clean and allows me to do amazing things. || You can find the source for the wrapper in plain-text here

    The only issue is that -- because of your DOM we have to do some structuring of all the items and group them, before we can iterate over those lists.

    We'll do this first by ->

    $.each($('h3'), function(i,v){
      $(v).nextUntil($('h3')).wrapAll('<div class="row-container"></div>');    
    });
    

    .nextUntil() is jQuery 1.6+, so hopefully there's no restrictions there.

    Now, with the plugin above that I've linked, we can reference it and have it wrap objects within each new row-container.

    $.each($('.row-container'), function(i,v){
      $(v).nwrapper({
        wrapEvery      : 3,
        defaultClasses : false,
        extraClasses: ['row']     
      });
    });
    

    The proof is in the pudding so here's the jsFiddle

    0 讨论(0)
  • 2020-12-18 03:43

    Try this. A simple solution..

    var h3s = $('h3');   // caching all h3 tags
    
    // looping over h3 tags
    $.each(h3s, function(i, hs) {
    
        // selecting div.item between two h3
        // for example
        // div.item between this (current h3) and h3:eq(1) (next h3) and so on
    
        var divs = $(this).nextUntil($('h3').eq(i+1), 'div.item');
    
        // looping with divs
        $.each(divs, function(i, el) {
    
            // checking for div.item
            // to group for wrapping
    
            if(i % 3 == 0) {
                divs.slice(i , i+3).wrapAll('<div class="row">');
            }
        });
    });
    

    Working sample

    Related refs:

    • .nextUntil()

    • wrapAll()

    • .slice()

    • .each()

    0 讨论(0)
  • 2020-12-18 03:46

    I've ended up with this and it's working

    $(function(){
        var h3=$('h3');
        h3.each(function(){
            var divs=$(this).nextUntil('h3');
            var row_wreapper=$('<div></div>');
            while(divs.length)
            {
                var grp=divs.splice(0, 3);
                var row=$('<div class="row"></div>');
                $(grp).each(function(){
                    row.append($(this));
                });
                row_wreapper.append(row);
            }
            $(this).after(row_wreapper.html());
        });
    });​
    

    DEMO or with a little extra checking of item class DEMO.

    Or

    $(function(){
        var h3=$('h3');
        h3.each(function(){
        var divs=$(this).nextUntil('h3');
        var row_wreapper=$('<div></div>');
        while(divs.length)
        {
            var grp=divs.splice(0, 3);
            var row=$(grp).wrapAll('<div class="row"></div>');
            if(row.children().length) row_wreapper.append(row);
        }
        $(this).after(row_wreapper.html());
        });
    });​
    

    DEMO.

    0 讨论(0)
提交回复
热议问题