文档准备好后,jQuery width()是否立即生效?

前端之家收集整理的这篇文章主要介绍了文档准备好后,jQuery width()是否立即生效?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
出于某种原因,$(“…”).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菜单项的宽度并调整它们的大小,以便它们始终适合我的响应式设计.没有图像或资产可能导致此更改.

更新
请参阅下面的评论.事实证明,有一种资产,一种嵌入式字体,需要一瞬间加载.

解决方法

这可能与第一个例子中缺少$(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.

原文链接:https://www.f2er.com/jquery/177677.html

猜你在找的jQuery相关文章