jQuery函数故障
我试图制作一个脚本,将容器中的元素放入列中。在我进入第4栏之前,脚本工作正常。我看不出我错在哪里。这是代码,还有jQuery函数故障,jquery,function,Jquery,Function,我试图制作一个脚本,将容器中的元素放入列中。在我进入第4栏之前,脚本工作正常。我看不出我错在哪里。这是代码,还有 var container='.bar', children=$(容器).children().length, 列=4, 宽度=$(容器).width()/列-20; 函数列生成器(值){ var i=1, x=数学单元(值/列), z=数学四舍五入(值/列), y=''; $(container.children().slice(0,x).wrapAll(y); while(i1)
var container='.bar',
children=$(容器).children().length,
列=4,
宽度=$(容器).width()/列-20;
函数列生成器(值){
var i=1,
x=数学单元(值/列),
z=数学四舍五入(值/列),
y='';
$(container.children().slice(0,x).wrapAll(y);
while(i1){
$(container.children().slice(i,x+i*i).wrapAll(y);
i++;
}
否则{
$(container.children().slice(i,x+i).wrapAll(y);
i++;
}
}
}
$(函数(){
变量容器='.bar',
children=$(容器).children().length,
列=4,
宽度=$(容器).width()/列-20;
函数列生成器(值){
var i=0,
x=数学单元(值/列),
z=数学四舍五入(值/列),
y='';
而(i<列){
$(容器).children(':not(“div.column”)).slice(0,x).wrapAll(y);
i++;
}
}
专栏作家(儿童);
$(容器)。追加(“”);
$('.column')。宽度(width);
});
此外,更改您的测试数据,使其在每次Lorum或DUI后都包含一个数字。否则,代码可能看起来像在工作,但实际上并非如此
这项技术也适用于任意数量的列(而不仅仅是4列)。如果你停止命名变量
x
、y
和z
,问题可能会更容易出现。我想我是把问题弄得太复杂了。谢谢你的帮助。
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 = '<div class="column" />';
$(container).children().slice(0, x).wrapAll(y);
while (i < column) {
if (value % 2 === 0 && z === 1 ) {
$(container).children().slice(i, x * i).wrapAll(y);
i++;
}
else if (value % 2 === 0 && z > 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 = '<div class="column" />';
while (i < column ) {
$(container).children(':not("div.column")').slice(0, x).wrapAll(y);
i++;
}
}
columnizer(children);
$(container).append("<div class='clear'></div>");
$('.column').width(width);
});