Is it safe to call $(document).ready() from inside a function?

前端 未结 3 1087
逝去的感伤
逝去的感伤 2021-01-06 01:35

If I use the $(document).ready() handler from within a function, will it still guarantee that the code inside it will only be run if the document is ready, even

3条回答
  •  抹茶落季
    2021-01-06 02:00

    Yes, that is safe. jQuery has several ways to set handlers like this, and the only "unsafe" one is $(document).bind("ready", handler). From the jQuery docs:

    All three of the following syntaxes are equivalent:

    1. $(document).ready(handler)
    2. $().ready(handler) (this is not recommended)
    3. $(handler)

    There is also $(document).bind("ready", handler). This behaves similarly to the ready method but with one exception: If the ready event has already fired and you try to .bind("ready") the bound handler will not be executed. Ready handlers bound this way are executed after any bound by the other three methods above.

提交回复
热议问题