jQuery width() incorrect immediately after document ready?

前端 未结 1 1626
野趣味
野趣味 2020-12-19 07:01

For some reason $(\"...\").width() is returning the wrong value immediately after document ready.

I\'m see these values:

Immediately after docum

相关标签:
1条回答
  • 2020-12-19 07:21

    It could be to do with the lack of $(window).load in the first example.

    "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");
    });
    

    *Quotes sourced from 4loc.

    0 讨论(0)
提交回复
热议问题