$(document).ready() fires too early

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

    I have absolutely, repeatably seen the same problem in IE9 and IE10. The jquery ready() call fires and one of my

    's does not exist. If I detect that and then call again after a brief timeout() it works fine.

    My solution, just to be safe, was two-fold:

    1. Append a at the end of the document then check for that variable in the ready() callback, AND

    2. Check if $('#lastElementInTheDocument').length > 0

    Yes, I recognize that these are nasty hacks. However, when ready() isn't working as expected some kind of work-around is needed!

    As an aside, the "correct" solution probably involves setting $.holdReady in the header, and clearing it at the end of the document. But of course, the really-correct solution is for ready() to work.

提交回复
热议问题