.slice and .wrapall

后端 未结 2 390
陌清茗
陌清茗 2021-01-18 21:43

I\'m using a bit of code suggested by a member on stackoverflow and adapted by me to wrap every 3 list items as part of a mega menu. The code is:

var lis = $         


        
2条回答
  •  迷失自我
    2021-01-18 22:20

    Your problem is your selector. Since sizzle works right to left, it will just query all LI elements which have an UL element as direct parent (which usually, is always the case).

    So, seperate your 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("
    "); } });

提交回复
热议问题