3 列のグリッド内の子要素の高さを動的に取得しようとしています。
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);
}