欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
  我们可以释放jQuery名字,传递true作为一个参数给这个方法。 这不是必须的,如果我们必须这样做的话(举个例子,如果我们在同一个页面上使用多个版本的jQuery库), 我们必须考虑到大多数插件依靠jQuery存在的变量,这种情况下,可能导致插件不能正常操作。
  
  例子:
  
  Example: 将$引用的对象映射回原始的对象。
  
  jQuery.noConflict();
  
  // Do something with jQuery
  
  jQuery("div p").hide();
  
  // Do something with another library's $()
  
  $("content").style.display = 'none';
  
  Example: 恢复使用别名$,然后创建并执行一个函数,在这个函数的作用域中仍然将$作为jQuery的别名来使用。在这个函数中,原来的$对象是无效的。这个函数对于大多数不依赖于其他库的插件都十分有效。
  
  jQuery.noConflict();
  
  (function($) {
  
  $(function() {
  
  // more code using $ as alias to jQuery
  
  });
  
  })(jQuery);
  
  // other code using $ as an alias to the other library
  
  Example: 你可以通过jQuery.noConflict() ready约束为一小段代码
  
  var j = jQuery.noConflict();
  
  // Do something with jQuery
  
  j("div p").hide();
  
  // Do something with another library's $()
  
  $("content").style.display = 'none';
  
  Example: 创建一个新的别名用以在接下来的库中使用jQuery对象。
  
  var dom = {};
  
  dom.query = jQuery.noConflict(true);
  
  Result:
  
  // Do something with the new jQuery
  
  dom.query("div p").hide();
  
  // Do something with another library's $()
  
  $("content").style.display = 'none';
  
  // Do something with another version of jQuery
  
  jQuery("div > p").hide();
  
  Example: Load two versions of jQuery (not recommended). Then, restore jQuery's globally scoped variables to the first loaded jQuery.
  
  <!DOCTYPE html>
  
  <html>
  
  <head>
  
  <script src="https://code.jquery.com/jquery-latest.js"></script>
  
  </head>
  
  <body>
  
  <div id="log">
  
  <h3>Before $.noConflict(true)</h3>
  
  </div>
  
  <script src="https://code.jquery.com/jquery-1.6.2.js"></script>
  
  <script>
  
  var $log = $( "#log" );
  
  $log.append( "2nd loaded jQuery version ($): " + $.fn.jquery + "<br>" );
  
  /*
  
  Restore globally scoped jQuery variables to the first version loaded
  
  (the newer version)
  
  */
  
  jq162 = jQuery.noConflict(true);
  
  $log.append( "<h3>After $.noConflict(true)</h3>" );
  
  $log.append( "1st loaded jQuery version ($): " + $.fn.jquery + "<br>" );
  
  $log.append( "2nd loaded jQuery version (jq162): " + jq162.fn.jquery + "<br>" );
  
  </script>
  
  </body>
  
  </html>

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h64060.shtml