Jquery height函数未正确报告内容div的高度
我有一个wordpress网站宽度和一个侧边栏,在那里客户想要一遍又一遍地重复一组广告来填补空白 因此,我对第一组广告进行了硬编码,然后使用一个函数获取#content DIV的高度,并循环遍历一组广告,并根据需要将它们附加到侧栏 它在网站的大部分页面上都能正常工作 但是由于某些原因,基于一个模板的页面没有报告正确的高度(我在页面上放了一个跟踪语句,它报告的高度是2500左右,而不是6000左右)。你可以在电视上看到 为了使调试更加困难,它会间歇性地在这些页面上工作。但通常情况下,重新加载后会再次失败。循环可以工作,只是次数不够,因为高度被错误地报告给脚本。我不明白 脚本如下:Jquery height函数未正确报告内容div的高度,jquery,wordpress,dom,for-loop,Jquery,Wordpress,Dom,For Loop,我有一个wordpress网站宽度和一个侧边栏,在那里客户想要一遍又一遍地重复一组广告来填补空白 因此,我对第一组广告进行了硬编码,然后使用一个函数获取#content DIV的高度,并循环遍历一组广告,并根据需要将它们附加到侧栏 它在网站的大部分页面上都能正常工作 但是由于某些原因,基于一个模板的页面没有报告正确的高度(我在页面上放了一个跟踪语句,它报告的高度是2500左右,而不是6000左右)。你可以在电视上看到 为了使调试更加困难,它会间歇性地在这些页面上工作。但通常情况下,重新加载后会再
<script type="text/javascript">
$(function() {
var contentHeight = $('#content').height();
var adList = [
'<a href="http://www.toveromarks.com"><img class="ad" src="http://happeninginthehills.com/wp-content/uploads/ads/ToveroHappeningHills.jpg" /></a>',
'<img class="ad" src="http://happeninginthehills.com/wp-content/uploads/2012/12/vi-owens-ad.png" />',
'<a href="http://www.hadunne.com"><img class="ad" src="http://happeninginthehills.com/wp-content/uploads/2012/12/susie-ad1.png" /></a>',
'<a href="http://www.deborahchabrian.com"><img class="ad" src="http://happeninginthehills.com/wp-content/uploads/ads/chabrian-ad.jpg" /></a>',
'<a href="http://www.tclaw.biz"><img class="ad" src="http://happeninginthehills.com/wp-content/uploads/2012/12/jeffs-ad.png" /></a>',
'<a href="http://www.edmartinezart.com"><img class="ad" src="http://happeninginthehills.com/wp-content/uploads/ads/martinez-ad.jpg" /></a>',
'<img class="ad" src="http://happeninginthehills.com/wp-content/uploads/2012/12/scott-phillips-ad.jpg" />'
];
var adHeight = 285;
var numRequiredAds = adList.length;
var heightDiff = contentHeight - (numRequiredAds * adHeight);
var numAds = Math.floor(heightDiff/adHeight);
if (heightDiff > adHeight){
for (var i = 0; i < numAds ; i++) {
$('#primary').append(adList[i % adList.length]);
//$('#primary').append('<p>ad num '+i+'</p>');
};
//$('#primary').append('<p>height is'+contentHeight+'</p>');
}
});
</script>
$(函数(){
var contentHeight=$('#content').height();
变量adList=[
'',
'',
'',
'',
'',
'',
''
];
八分之八=285;
var numRequiredAds=adList.length;
var heightDiff=内容高度-(numRequiredAds*AdHight);
var numAds=数学楼层(高度差/高度);
如果(高度差>高度){
对于(变量i=0;i);
};
//$(“#primary”).append(“高度为“+contentHeight+””);
}
});
混淆了哪个元素没有报告正确的高度?对不起,$(“#content”).height()代码>就是那个。div id内容是我正在匹配其高度的主div。是否在document.ready中执行此操作?或window.onload。如果图像未加载到Good point,则可能还无法获得图像的高度。我应该使用哪个?对于图像,请使用window.onload,尤其是在chrome或safari文档中。ready应与FF一起使用
window.onload = function() {
// your code
}