这是我得到的并且花了几个小时来挣扎。如果我在每个循环中使用 alert(i)
,它会给我返回 1,2,3...
,但是如果我想要将其作为多维数组的键,它就像一个字符串 "i"
。
$(document).ready(function(){
var positions=[];
$( ".box" ).each(function(i) {
//alert(i);
var elPositions = {};
elPositions.i = $(this).offset().top;
positions.push(elPositions);
//$elPosArray[i] = $(this).offset().top;
//$(this).html('outer height--> ' + $(this).outerHeight(true));
});
console.log(positions);
//console.log(el);
});
这个话题有很多问题和回答,但是没有一个能帮我解决问题。
我想要得到一个类似于以下样式的数组或对象:
positions[0]['offset'] = '120';
positions[0]['height'] = '300';
positions[1]['offset'] = '420';
positions[1]['height'] = '180';
positions[2]['offset'] = '600';
positions[2]['height'] = '100';
positions[3]['offset'] = '700';
positions[3]['height'] = '300';
这是一个包含HTML的Fiddle示例,链接为http://jsfiddle.net/Z9WrG/。