$(document).ready简写 [英] $(document).ready shorthand
问题描述
以下是 $(文件).ready
的简写?
(function($){
//some code
})(jQuery);
我看到这种模式使用了很多,但我找不到任何引用。如果它是 $(文件).ready()
的简写,是否有任何特殊原因可能无效?在我的测试中,似乎总是在准备好的事件之前触发。
I see this pattern used a lot, but I'm unable to find any reference to it. If it is shorthand for $(document).ready()
, is there any particular reason it might not work? In my tests it seems to always fire before the ready event.
推荐答案
$的简写(文件).ready(处理程序)
是 $(处理程序)
(其中处理程序
是一个函数)。请参见此处。
The shorthand for $(document).ready(handler)
is $(handler)
(where handler
is a function). See here.
您的问题中的代码与您无关。就绪()
。相反,它是一个立即调用的函数表达式(IIFE),其中jQuery对象作为其参数。其目的是将至少 $
变量的范围限制为它自己的块,这样就不会引起冲突。您通常会看到jQuery插件使用的模式,以确保 $ == jQuery
。
The code in your question has nothing to do with .ready()
. Rather, it is an immediately-invoked function expression (IIFE) with the jQuery object as its argument. Its purpose is to restrict the scope of at least the $
variable to its own block so it doesn't cause conflicts. You typically see the pattern used by jQuery plugins to ensure that $ == jQuery
.
这篇关于$(document).ready简写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!