问题描述
我有一些代码,可以在页面加载时获取图像的高度,然后相应地调整其容器div的大小.除非它是第一次加载该页面,否则它运行良好,除非我使用ctrl + f5进行了硬刷新,然后它不起作用.我尝试使用
I have some code that when the page loads gets the height of an image and then resizes its container div accordingly. This works fine unless its the first time the page has been loaded or i do a hard refresh with ctrl + f5, then it doesn't work. I have tried using
$('#div img').load(function() {
// put the code here
});
但是我遇到了同样的问题.有人知道为什么会这样吗?
But I get the same problem. Anyone know why this is happening?
这是我的代码:
maxheight = 0;
$('#venue #main-img img').each(function() {
height = $(this).height();
if(height > maxheight) {
maxheight = height;
}
$(this).hide();
});
$('#venue #main-img').animate({ height: maxheight });
$('#venue #main-img img').first().show();
对不起,我应该说.代码在$(document).ready()
Sorry should have said. The code is within $(document).ready()
推荐答案
尝试在$(window).load
中调用您的代码.这将在所有图像加载后运行.
Try calling your code in $(window).load
. This will run after all images have loaded.
$(window).load(function () {
// run code
});
或者您可以尝试 imgload
插件.
Or you could try the imgload
plugin.
这篇关于硬刷新后jquery .load()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!