$(document).ready() fires too early

前端 未结 7 1638
青春惊慌失措
青春惊慌失措 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:13

    If you rely on external content to be already loaded (e.g. images, fonts), you need to use the window.load event

    $(window).on("load", function() {
        // code here
    });
    

    The behaviour of these events is described in this article:

    There is [a] ready-state however known as DOM-ready. This is when the browser has actually constructed the page but still may need to grab a few images or flash files.

    Edit: changed syntax to also work with jQuery 3.0, as noted by Alex H

提交回复
热议问题