我的js页面中有两个html文件。如果第一个以:

 (function($){
   ..
   ..
   }(jQuery));

我可以在var中插入function($,varname)return是它的值并在其他文件中使用它吗?

最佳答案

为此,您需要一个全局变量。您可以通过以下几种方式之一进行操作。假设我们需要将值“Bacon”发送到其他脚本。

(function($){
   window.myScriptsExports = "Bacon";
}(jQuery));

// OR

var myScriptsExports = (function($){
   // other code
   return "Bacon";
   // NO other code
}(jQuery));

// OR (not really recommended)

(function($){
   // other code
   $.myScriptsExports = "Bacon";
   // other code
}(jQuery));

09-07 15:30
查看更多