gpt4 book ai didi

Chrome 中的 JQuery 窗口宽度不正确

转载 作者:行者123 更新时间:2023-12-03 22:41:06 25 4
gpt4 key购买 nike

我创建了一个插件,我已将其简化为插件的骨架(即没有功能,只有结构)来调试此问题,但它仍然存在。它处理调整 div 的大小并基于文档的宽度。

问题是文档报告的宽度比实际宽度减少了 17px,因此无法正确调整 div 的大小。

我已发出警报以找出问题发生的位置:在“准备好”功能之前:1780px(正确宽度)在“ready”函数内部:1763px插件内:1763px(从“document.width”和“$(document).width()”返回的所有值具有相同的结果)

这种情况只发生在 Chrome 中,即使插件减少到几乎没有,这种情况仍然存在。

对于为什么会发生这种情况有什么想法吗?

最佳答案

取自jQuery documentation :

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. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code. When using scripts that rely on the value of CSS style properties, it's important to reference external stylesheets or embed style elements before referencing the scripts.

In cases where code relies on loaded assets (for example, if the dimensions of an image are required), the code should be placed in a handler for the load event instead.

你可能应该使用

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

作为触发事件

关于Chrome 中的 JQuery 窗口宽度不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/887931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com