gpt4 book ai didi

文档准备好后 jQuery width() 立即不正确?

转载 作者:太空狗 更新时间:2023-10-29 14:00:56 25 4
gpt4 key购买 nike

由于某些原因 $("...").width() 在文档准备好后立即返回错误值。

我看到了这些值:

文档准备好后立即:

$(document).ready(function(){
$("li.active a").width() //returns 76 - incorrect
});

$(document).ready(function(){
$(window).load(function(){
$("li.active a").width() //returns 59 - the correct value
});
});

$(document).ready(function(){
setTimeout(function(){
$("li.active a").width() //returns 59 - the correct value
}, 100);
});

我正在获取 wordpress 菜单项的宽度并调整它们的大小,以便它们始终适合我的响应式设计。没有应该导致此更改的图像或 Assets 。

更新请参阅下面的评论。原来有一个 Assets ,一个嵌入的字体,需要瞬间加载。

最佳答案

这可能与第一个示例中缺少 $(window).load 有关。

"The document ready event executes already when the HTML-Document is loaded and the DOM is ready, even if all the graphics haven’t loaded yet. If you want to hook up your events for certain elements before the window loads, then $(document).ready is the right place." *

$(document).ready(function() {
// executes when HTML-Document is loaded and DOM is ready
alert("document is ready");
});

"The window load event executes a bit later when the complete page is fully loaded, including all frames, objects and images. Therefore functions which concern images or other page contents should be placed in the load event for the window or the content tag itself." *

$(window).load(function() {
// executes when complete page is fully loaded (all frames, objects and images)
alert("window is loaded");
});

*引用自 4loc .

关于文档准备好后 jQuery width() 立即不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15668611/

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