脚本仅在页面重新加载时工作 [英] Script working only on page reload
问题描述
我使用jQuery在水平视图网站上对齐图像,但脚本只在我重新加载页面时有效。
http://joliannelm.steveforest.com/edition/roux-de-service.html
脚本之前调用
这是脚本:
$(document).ready(function(){
var width = 0;
$ ').each(function(){
width + = $(this).outerWidth(true);
});
$('#page')。css ('width',width + 50);
});
页面重新加载后就可以了,但是如果你清除缓存, / p>
有人知道为什么?感谢。
这可能是一个时间问题:准备
在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屋!