程序員論壇

 找回密碼
 立即註冊
搜索
熱搜: 活動 交友 discuz
查看: 1594|回復: 0
打印 上一主題 下一主題

jQuery.noConflict()

[複製鏈接]

8166

主題

1

好友

2萬

積分

版主

Rank: 7Rank: 7Rank: 7

跳轉到指定樓層
樓主
發表於 2020-2-27 11:42:38 |只看該作者 |倒序瀏覽
Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $. If you need to use another JavaScript library alongside jQuery, return control of $ back to the other library with a call to $.noConflict(). Old references of $ are saved during jQuery initialization; noConflict() simply restores them.

就像jQuery一樣,許多JavaScript庫都將$用作函數或變量名。 在jQuery的情況下,$只是jQuery的別名,因此無需使用$即可使用所有功能。 如果您需要在jQuery旁邊使用另一個JavaScript庫,請通過調用$ .noConflict()將$的控制權返回到另一個庫。 $的舊引用在jQuery初始化期間保存; noConflict()只是還原它們。
  1. <script src="other_lib.js"></script>
  2. <script src="jquery.js"></script>
  3. <script>
  4. $.noConflict();
  5. // Code that uses other library's $ can follow here.
  6. </script>
複製代碼
  1. <script src="other_lib.js"></script>
  2. <script src="jquery.js"></script>
  3. <script>
  4. $.noConflict();
  5. jQuery( document ).ready(function( $ ) {
  6.   // Code that uses jQuery's $ can follow here.
  7. });
  8. // Code that uses other library's $ can follow here.
  9. </script>
複製代碼
  1. jQuery.noConflict();
  2. // Do something with jQuery
  3. jQuery( "div p" ).hide();
  4. // Do something with another library's $()
  5. $( "content" ).style.display = "none";
複製代碼
  1. jQuery.noConflict();
  2. (function( $ ) {
  3.   $(function() {
  4.     // More code using $ as alias to jQuery
  5.   });
  6. })(jQuery);
複製代碼
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

程序員論壇

GMT+8, 2024-9-21 00:40 , Processed in 0.049408 second(s), 19 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回頂部
HK Piece免費建立你的網上商店
Pocket Money Easy