$(document).ready() fires too early

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

    The problem $(document).ready() fires too early can happen sometimes because you've declared the jQuery onReady function improperly.

    If having problems, make sure your function is declared exactly like so:

     $(document).ready(function() 
     {
       // put your code here for what you want to do when the page loads.
     });
    

    For example, if you've forgotten the anonymous function part, the code will still run, but it will run "out of order".

    console.log('1');
    $(document).ready()
    {
      console.log('3');
    }
    console.log('2');
    

    this will output

    1
    3
    2
    

提交回复
热议问题