$(document).ready() fires too early

前端 未结 7 1583
青春惊慌失措
青春惊慌失措 2020-12-30 19:29

So, I need to know the width of an element with javascript, the problem I have is that the function fires too early and the width changes when the css is tottally applied. A

7条回答
  •  别那么骄傲
    2020-12-30 20:34

    Quote OP:

    "As I understood, the $(document).ready() function was fired when the document is completed,"

    $(document).ready() fires when the DOM ("document object model") is fully loaded and ready to be manipulated. The DOM is not the same as the "document".

    W3C - DOM Frequently Asked Questions

    You can try $(window).load() function instead...

    $(window).load(function() {
        // your code
    });
    

    It will wait for all the page's assets (like images and fonts, etc.) to fully load before firing.

提交回复
热议问题