闭包 – jQuery的noConflict:快速思考

文档说你可以像这样使用$.noConflict():
jQuery.noConflict();
(function($) { 
  $(function() {
    // more code using $as alias to jQuery
  });
})(jQuery);
// other code using $as an alias to the other library

它还声明调用它返回jQuery对象的实例,所以我可以这样做:

jQuery.noConflict()(function(){
    // code using jQuery
}); 
// other code using $as an alias to the other library

但是,这种组合有效吗?

(function($) { 
  $(function() {
    // more code using $as alias to jQuery
  });
})(jQuery.noConflict());
// other code using $as an alias to the other library

如果是这样,有没有理由不这样做?而且(如果它工作),为什么不总是使用这个方法来保证我们的闭包内部,$== jQuery?

解决方法

最后一个方法也可以 – jQuery.noConflict()返回jQuery对象,该对象作为$参数传递给函数.

我没有理由不这样做,并且更喜欢其他方法.

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: <span id=&quot...
jQuery 添加水印 <script src="../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...