我想根据其高度对列中的某些图像进行排序,并且我有以下代码。 问题是即使imgHeights.length工作正常,imgHeights [i](在redrawThumbs函数中)不会返回值。 我做错了什么?感谢=)JavaScript无法从函数的数组参数中获取值
function redrawThumbs(imgHeights){
// some irrelevant code here
// initialise an array that will hold all the column's heights
var colHeights = new Array(cols);
for(a=0; a <= colHeights.length - 1; ++a){
colHeights[a] = 0;
}
// take each image's height and add it to the shortest column
for(i=0; i <= imgHeights.length - 1; ++i){
var shortestCol = 0;
for(c=0; c <= colHeights.length - 2; ++c){
if(colHeights[c+1] < colHeights[c]){
shortestCol = colHeights[c+1];
}
}
alert("imgHeights[" + i + "] " + imgHeights[i]);
colHeights[shortestCol] += imgHeights[i];
}
}
// make an array of image heights
var imgHeights = new Array(totalThumbs);
for(i=1; i <= totalThumbs; ++i){
var img = new Image();
img.onload = function(){
imgHeights[i-1] = this.height;
}
img.src = i + ".jpg";
// call function that orders the images
redrawThumbs(imgHeights);
您可能想要在代码中添加缩进,并添加缺少的}。很难判断你的'redrawThumbs()'方法在哪里结束。 – jaredhoyt
我加了缺少的}。 indentation ... 2空间不是那样的小代码不好:D 我刚才注意到最怪异的事情,而调试是底部为我在哪里创建imgHeights阵列,以及我把一个警报在那里打印我的变量和它总是显示循环的最后一个数字。我不知道为什么会发生这种情况,因为它使用它创建元素时工作正常 –