html部分如下
js如下
var $ul = $('#section5 .scrollBox1 .bd ul'); $ul.each(function () { var $nul = $('
'); var $curUl = $(this); $curUl.children().each(function (idx) { console.log(idx); $(this).appendTo($nul); if ((idx + 1) % 4 === 0) { $nul.appendTo($curUl.parent()); $nul = $('
'); } }); if ($nul.children().length != 4) { $nul.appendTo($curUl.parent()); } $curUl.remove(); });
这里面,我不太明白写idx
是什么意思?我用console看过,结果是这样
所以这个idx到底是什么??另外这句if ((idx + 1) % 4 === 0)
又该怎么理解呢?
http://www.css88.com/jqapi-1....
为每个匹配元素执行的一个函数。
比如你的ul有六个li,index为序号,从0开始
if ((idx + 1) % 4 === 0)
就是第四个(或者四的倍数个)就执行条件的内容。(+1变成 从1开始算,能被4整除,不就是4,8,12那些了么)each api看最下面,第一个参数是遍历的索引。
然后
(idx + 1) % 4 === 0
什么意思我觉得你就懂了,就是索引+1除以4,有没有余数的意思。