Wrap every 3 divs in a div

前端 未结 6 1866
执念已碎
执念已碎 2020-11-22 09:03

Is it possible to use nth-child selectors to wrap 3 divs using .wrapAll? I can\'t seem to work out the correct equation.

so...



        
6条回答
  •  孤街浪徒
    2020-11-22 09:41

    I have prepared this answer for another question that was duplicate of this one. So maybe my variant will be usefull for some one:

    I think the solution to wrap all three elements is:

    var $lines = $('.w-col'), // All Dom elelements with class .w-col
         holder = []; //Collect DOM elelements
    
    $lines.each(function (i, item) {
      holder.push(item);
    
      if (holder.length === 3) {
        $(holder).wrapAll('
    '); holder.length = 0; } }); $(holder).wrapAll('
    '); //Wrap last elements with div(class=w-row)

    I have wrote the same code at jsbin with some improvements http://jsbin.com/necozu/17/ or http://jsbin.com/necozu/16/

提交回复
热议问题