大家好,我在 jquery 中有一小段代码,但我遇到了问题。似乎for循环跳过了第二个参数(当i = 2时),你能告诉我什么问题吗?
这是代码:
var items = $(".item").length;
var currentIndex = items;
place(currentIndex);
function place(index){
var s1 = Math.floor(items / 2);
for (i = 1; i <= items; i++){
(function(i, index){
if (i <= s1){
var id = findNext(1, i);
console.log("i = " + i + " > id = " + id);
} else if ( i > s1){
console.log("i = " + i);
}
})(i, index);
}
}
function findNext(index, times){
var result = index;
for (i = 1; i <= times; i++){
if (result == items){
result = 1;
} else {
result ++;
}
}
return result;
}
控制台输出显示:
i = 1 > id = 2
i = 3
i = 4
所以似乎 for 循环跳过了第二个参数(当 i = 2 时)你能告诉我有什么问题吗?