简体   繁体   English

jQuery 在 Chrome 中的页面刷新 (F5) 后表现异常

[英]jQuery behaving strange after page refresh (F5) in Chrome

I have a script, that works fine in FireFox, IE, but not in Chrome after F5 refresh only, It does work in chrome, if I focus the url address and click enter, but it doesnt work if I press F5, I don't get it at all.我有一个脚本,在 FireFox,IE 中运行良好,但在 F5 刷新后不能在 Chrome 中运行一点也不明白。 how can this have an effect on the jQuery code.这对 jQuery 代码有何影响。

So here's the script:所以这是脚本:

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

    $('#offer #gallery').css('width', width + 'px');
});

And the result of Width should be 820, but I only get this if I refresh the site by focusing the url address and clicking enter, otherwise it gives the result of 90. Width 的结果应该是 820,但只有当我通过聚焦 url 地址并单击 Enter 来刷新站点时才会得到这个,否则它会给出 90 的结果。

I tryed restarting browser and deleting cache, so the problem isn't there, any ideas?我尝试重新启动浏览器并删除缓存,所以问题不存在,有什么想法吗?

To correct this problem use:要纠正此问题,请使用:

$(window).load(function() {});

Out instead of:出而不是:

$(document).ready(function() {});

I'd guess that this happens because the images have not yet been loaded when the ready() event is triggered, and 18px is the size of the placeholder icon for images.我猜这是因为触发 ready() 事件时图像尚未加载,而 18px 是图像占位符图标的大小。 Pressing F5 causes all resources in the page to be reloaded, while normal navigation does not.按 F5 会重新加载页面中的所有资源,而普通导航不会。

You could try replacing the ready() call with a load() call, or supplying width and height attributes in your img tags so that their size is known before the images are loaded.您可以尝试用 load() 调用替换 ready() 调用,或者在 img 标签中提供宽度和高度属性,以便在加载图像之前知道它们的大小。

From JQuery API for.load() :JQuery API for.load()

The load event is sent to an element when it and all sub-elements have been completely loaded.当元素和所有子元素都已完全加载时,加载事件将发送到该元素。

And from .ready() :.ready()

While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received.虽然 JavaScript 在呈现页面时提供了用于执行代码的加载事件,但在完全接收到图像等所有资产之前不会触发此事件。 In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed.在大多数情况下,只要完全构建了 DOM 层次结构,就可以运行脚本。

I had a similar problem trying to call a custom js library.我在尝试调用自定义 js 库时遇到了类似的问题。 I was also using a combination of jquery and jquerymobile.我还使用了 jquery 和 jquerymobile 的组合。

Both $(window).load(function() {});两者$(window).load(function() {}); and $(document).ready(function() {});$(document).ready(function() {}); were failing.失败了。 Using:使用:

$(document).on("pageinit", function () {});

solved my case.解决了我的情况。

If you have jQuery 3.0 and later, use:如果您有 jQuery 3.0 及更高版本,请使用:

 $(window).on("load", function(){ // Do your work });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM