jQuery wrap every X elements in div

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

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

<
4条回答
  •  离开以前
    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('
    '); });

    .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

提交回复
热议问题