.slice and .wrapall

I use a bit of code suggested by a member on stackoverflow and adapted by me to wrap all 3 list items as part of a mega menu. The code:

var lis = $("ul > li");
for(var i = 0; i < ls.length; i+=3) {
  lis.slice(i, i+3).wrapAll("<div class='new'></div>");
}

Unfortunately, this will lead to capturing the child li from the next parent menu to fill the quota of 3 li in the div. This, of course, massively ruin my menu. For an example, please visit here.

Does anyone have a suggestion how I can fix this?

+5
source share
2 answers

Your problem is your selector. Since it sizzleworks from right to left, it simply asks for everything LI elementsthat has UL elementa direct parent (usually this is always the case).

, ULs.

$('ul').each(function(){
   var $lis = $(this).children('li');
   for(var i = 0, len = $lis.length; i < len; i+=3){          
     $lis.slice(i, i+3).wrapAll("<div class='new'></div>");
  }
});
+6

ht?

var lis = $("ul.list-content > li");
for(var i = 0; i < lis.length; i+=3) {
  lis.slice(i, i+3).wrapAll("<div class='new'></div>");
}

, , . div ul, ..;)

0

All Articles