jQueryfunction麻烦

我正在尝试制作一个脚本,将元素放入容器中。 该脚本正常工作,直到我到达4列。 我看不出哪里出错了。 这是代码和演示

var container = '.bar', children = $(container).children().length, column = 4, width = $(container).width() / column - 20; function columnizer(value) { var i = 1, x = Math.ceil(value / column), z = Math.round(value / column), y = '
'; $(container).children().slice(0, x).wrapAll(y); while (i 1) { $(container).children().slice(i, x + i * i).wrapAll(y); i++; } else { $(container).children().slice(i, x + i).wrapAll(y); i++; } } }

 $(function() { var container = '.bar', children = $(container).children().length, column = 4, width = $(container).width() / column - 20; function columnizer(value) { var i = 0, x = Math.ceil(value / column), z = Math.round(value / column), y = '
'; while (i < column ) { $(container).children(':not("div.column")').slice(0, x).wrapAll(y); i++; } } columnizer(children); $(container).append("
"); $('.column').width(width); });

此外,更改您的测试数据以包含每个Lorum或Duis之后的数字。 否则,代码可能看起来像它的工作,但实际上不是。

此技术也适用于任意数量的列(而不仅仅是4列)。