.slice和.wrapall [英] .slice and .wrapall

查看:146
本文介绍了.slice和.wrapall的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在stackoverflow上使用了一位成员建议的代码,并由我进行了修改,以将每3个列表项包装为大型菜单的一部分.代码是:

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

不幸的是,这将从下一个父菜单中获取子li来填充div中3 li的"quota".当然,这极大地弄乱了我的菜单. 有关示例,请访问此处.

Unfortunately this will grab child li's from the next parent menu to fill up the 'quota' of 3 li's in a div. This is of course massively messing up my menus. For an example please visit here.

有人建议我如何解决这个问题吗?

Does anyone have any suggestion how I could fix this up?

推荐答案

您的问题是您的选择器.由于sizzle从右到左起作用,因此它将仅查询具有UL element作为直接父级的所有LI elements(通常总是这样).

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).

所以,请分开您的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>");
  }
});

这篇关于.slice和.wrapall的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆