脚本仅在页面重新加载时工作 [英] Script working only on page reload
问题描述
我正在使用jQuery在水平视图网站上对齐图像,但脚本仅在我重新加载页面时有效。
http://joliannelm.steveforest.com/edition/roux-de-service.html
脚本在
之前调用这是脚本:
($($)$($)$($)$($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ ').each(function(){
width + = $(this).outerWidth(true);
});
$('#page')。 ('width',width + 50);
});
一旦页面被重新加载就可以了,但是如果你清除缓存,它回来了...
有人知道为什么?谢谢。
这可能是一个时间问题: ready
当DOM准备就绪时被触发 - 这可以在加载图像之前。因为您没有给出固定的 width
属性的图像,所以您需要先将它们加载,然后才能确定宽度。
这是 加载
事件是最合适的触发器。
$(window).load(function(){...你的代码在这里
不像准备好
,加载
将等待所有相关的文件 - 图像,样式表,JavaScript文件等。
I'm using jQuery to align images on an horizontal view website, but the script only works when I reload the page.
http://joliannelm.steveforest.com/edition/roux-de-service.html
The script is called just before
This is the script :
$(document).ready(function() {
var width = 0;
$('#page img').each(function() {
width += $(this).outerWidth(true);
});
$('#page').css('width', width + 50);
});
Once the page is reloaded it's OK, but if you clear the cache, it's back...
Someone knows why?? Thanks.
This is probably a timing problem: The ready
event is fired when the DOM is ready - this can be before the images are loaded. Because you're not giving the images fixed width
attributes, you need them to be loaded before you can tell their width.
This is where the load
event is the most appropriate trigger.
$(window).load(function() { ... your code here
unlike ready
, load
will wait for all associated files - images, style sheets, JavaScript files etc.
这篇关于脚本仅在页面重新加载时工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!