如何:
(function($) {
/// code
})(jQuery)
不同于 jquery 中的 $(document).ready(function()
?
我知道ready
函数的作用。它会等到 HTML 加载完毕后才会开始。然而,(function($)
做同样的事情吗?
最佳答案
I know what the ready function does. It awaits until the HTML is loaded before it starts. However, does
(function($) { ... })()
do the same?
不,它没有。当(以及每当)控制到达该语句时,它会立即执行。
尝试运行
$(document).ready(function() { alert('happens second'); });
(function($) {
alert('happens first');
})(jQuery);
亲眼目睹。
关于javascript - jQuery 函数语法差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9763466/