jQuery(function(){})与(function(){})(jQuery)的区别
Posted on 2013-01-06 09:50 bw_0927 阅读(119) 评论(0) 收藏 举报http://eason26-li.iteye.com/blog/520188
开发jQuery插件时总结的一些经验分享一下。
一、先看
jQuery(function(){
});
全写为
jQuery(document).ready(function(){
});
意义为在DOM加载完毕后执行了ready()方法。
二、再看
(function(){
})(jQuery);
其实际上是执行()(para)匿名方法,只不过是传递了jQuery对象。
三、总结
jQuery(function(){ });用于存放操作DOM对象的代码,执行其中代码时DOM对象已存在。不可用于存放开发插件的代码,因为jQuery对象没有得到传递,外部通过jQuery.method也调用不了其中的方法(函数)。
(function(){ })(jQuery);用于存放开发插件的代码,执行其中代码时DOM不一定存在,所以直接自动执行DOM操作的代码请小心使用。
=====
$(function(){...});
This is shorthand for:
$(document).ready(function(){...});
What it does is registering a handler for the ready
event, so the code in the function will be run as soon as the document has loaded.