我想設置div#頂部高度等於圖像高度。我寫道:在文檔加載時獲取圖像高度
$(document).ready(function() {
setTopHeight();
});
/*
* window resizing
*/
$(window).resize(function() {
setTopHeight();
});
/*
* setting #top height by banner height
*/
var setTopHeight = function() {
var bannerHeight = $('#banner-image').height();
$('#top').height(bannerHeight + 'px');
};
它適用於調整大小,但不重新加載。我試過這樣:
$(document).ready(function() {
setTimeout(function() {
setTopHeight();
}, 50);
});
它的工作原理,但它當然不是解決方案。有人可以告訴我爲什麼console.log返回0嗎?
$(document).ready(function() {
console.log($('#banner-image').height());
});
是banner-image img標籤的id或它的圖像包裝div ID? – 2013-03-20 10:57:36