我正在尝试动态获取三列网格内子元素的高度。
当i
等于 2 或i
大于 1 时(即当循环内至少有 2 个元素时),offsetHeight
正确返回渲染高度(我在专用元素中显示渲染高度值以$('#testheight')
进行检查。)
但是,当i
等于 1 时,offsetHeight
返回 0,即使呈现的元素具有高度(<img>
通过 PHP 在子元素内部呈现了一个元素)。
我找不到错误!请帮忙!
function makeGrid(){
var blocks = document.getElementById("grid_container").children;
var pad = 0, cols = 3, newleft, newtop;
var max_height = 0;
var newoffsetheight = 0;
for(var i = 1; i < blocks.length; i++){
if (i % cols == 0) {
newtop = (blocks[i-cols].offsetTop + blocks[i-cols].offsetHeight) + pad;
max_height = Math.max(max_height, newtop + blocks[i-cols].offsetHeight);
blocks[i].style.top = newtop+"px";
newoffsetheight = blocks[i].offsetHeight;
}
else {
if(blocks[i-cols]){
newtop = (blocks[i-cols].offsetTop + blocks[i-cols].offsetHeight) + pad;
blocks[i].style.top = newtop+"px";
}
newleft = (blocks[i-1].offsetLeft + blocks[i-1].offsetWidth) + pad;
blocks[i].style.left = newleft+"px";
newoffsetheight = blocks[i].offsetHeight;
}
}
$('#testheight').html(newoffsetheight);
}