脚本只能在页面重新加载(Script working only on page reload)

编程入门 行业动态 更新时间:2024-10-21 15:37:26
脚本只能在页面重新加载(Script working only on page reload)

我使用jQuery在水平视图网站上对齐图像,但该脚本仅在我重新加载页面时才起作用。

http://joliannelm.steveforest.com/edition/roux-de-service.html

该脚本之前被调用

这是脚本:

$(document).ready(function() { var width = 0; $('#page img').each(function() { width += $(this).outerWidth(true); }); $('#page').css('width', width + 50); });

一旦页面被重新加载,这是可以的,但如果你清除缓存,它会回来...

有人知道为什么? 谢谢。

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.

最满意答案

这可能是一个计时问题: ready事件是在DOM准备好时触发的 - 这可以在加载图像之前完成。 因为你没有给出图像的固定width属性,所以你需要先加载它们,然后才能说出它们的宽度。

这是load事件是最合适的触发器。

$(window).load(function() { ... your code here

不像ready , load将等待所有相关文件 - 图像,样式表,JavaScript文件等。

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.

更多推荐

本文发布于:2023-08-06 17:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454091.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   加载   页面   只能在   reload

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!