3

我对它的实际含义感到非常困惑

(function ($){})(jQuery) 
//in plugin

$(function (){})
//in page.

请让我明白这一点。

4

3 回答 3

5

这个:

(function ($){})(jQuery) 

...是一个被定义然后立即调用的函数,其中 JQuery 对象作为参数传入。这$是对 JQuery 的引用,然后您可以在函数中使用它。这相当于:

var myFunc = function ($){};
myFunc(jQuery);

这个:

$(function (){})

...是JQuery 的调用,传入一个函数,一旦文档完成加载,它应该执行该函数。

于 2012-09-11T08:04:06.620 回答
1
$(function(){}); === $(document).ready(function(){});. 

以上两者都是一样的。

其中,(function($){ .... })(jQuery);是编写插件的结构。

于 2012-09-11T07:14:20.947 回答
0

这两者是不一样的。下面将清楚地解释每一件事,

(function($){
  /* code here runs instantly*/
  $('document').ready(function(){ // this function is exactly the same as the one below
        /* code here runs when dom is ready */
  });
  $(function(){ // this function is exactly the same as the one above.
        /* code here runs when dom is ready */
  }
)(jQuery); // jQuery is a parameter of function($) {}

参考:http: //forum.jquery.com/topic/what-s-the-difference-between-function-code-jquery-and-document-ready-function-code

于 2012-09-11T07:25:51.117 回答