自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

兼容第三方框架 jQuery多庫共存機(jī)制詳解

開發(fā) 開發(fā)工具
jQuery多庫共存機(jī)制指jQuery庫完全兼容第三方庫,例如jQuery中使用$做為函數(shù)入口,在該頁面同時(shí)引入另一個(gè)庫,其中也使用了$做為函數(shù)名。

在Web項(xiàng)目開發(fā)中,經(jīng)常需要引用第三方JavaScript庫,如果第三方JavaScript庫與自已的JavaScript庫使用相同的全局變量,是一個(gè)比較麻煩的事。程序員多半可能會(huì)修改其中一方的JavaScript代碼。能不能有一個(gè)比較好的方法解決呢?讓我們看一下jQuery如何做到的。

jQuery多庫共存機(jī)制指jQuery庫完全兼容第三方庫,例如jQuery中使用$做為函數(shù)入口,在該頁面同時(shí)引入另一個(gè)庫,其中也使用了$做為函數(shù)名。因此jQuery與該庫發(fā)生沖突,例1:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <HTML> 
  3.  <HEAD> 
  4.   <TITLE> New Document </TITLE> 
  5.     <script src = "http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" language = "javascript"></script> 
  6.     <SCRIPT LANGUAGE="JavaScript"> 
  7.   <!--  
  8.  //第三方庫  
  9.   function $(str)  
  10.   {  
  11.   return document.getElementById(str) ;  
  12.   }  
  13.  
  14.   function jQuery(str)  
  15.   {  
  16.   return document.getElementById(str) ;  
  17.   }  
  18.   //--> 
  19.   </SCRIPT> 
  20.  </HEAD> 
  21.  
  22.  <BODY> 
  23.  <input type = "text" id = "txt1" value = "aa" /> 
  24.  </BODY> 
  25. </HTML> 

在如上示例中 第三方庫同時(shí)使用了"$"與"jQuery",此時(shí)jQuery入口被第三方庫覆蓋了。jQuery提供了noConflict函數(shù)解決沖突,例2:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <HTML> 
  3.  <HEAD> 
  4.   <TITLE> New Document </TITLE> 
  5.     <script src = "http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" language = "javascript"></script> 
  6.  <script> 
  7.   //兼容代碼  
  8.   var $1 = $.noConflict();  
  9.   $1(document).ready(function(){  
  10.    alert($1("#txt1").val())  
  11.    alert($("txt1").value) ;  
  12.   })  
  13.  </script> 
  14.     <SCRIPT LANGUAGE="JavaScript"> 
  15.   <!--  
  16.  //第三方庫  
  17.   function $(str)  
  18.   {  
  19.   return document.getElementById(str) ;  
  20.   }  
  21.  
  22.   function jQuery(str)  
  23.   {  
  24.   return document.getElementById(str) ;  
  25.   }  
  26.   //--> 
  27.   </SCRIPT> 
  28.  </HEAD> 
  29.  
  30.  <BODY> 
  31.  <input type = "text" id = "txt1" value = "aa" /> 
  32.  </BODY> 
  33. </HTML> 

noConflict重新將jQuery入口指針指向$1,此時(shí)可以用$1訪問jQuery庫,其中兼容代碼要寫在第三方庫載入之前(如果寫在之后,jQuery的$和jQuery入口被第三方庫覆蓋了,無法調(diào)用兼容代碼)。

在實(shí)際應(yīng)用中,如果jQuery載入位置在第三方庫之后,jQuery會(huì)覆蓋第三方JavaScript庫么?如下代碼,例3:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <HTML> 
  3.  <HEAD> 
  4.   <TITLE> New Document </TITLE> 
  5.     <SCRIPT LANGUAGE="JavaScript"> 
  6.   <!--  
  7.  //第三方庫  
  8.   function $(str)  
  9.   {  
  10.   return document.getElementById(str) ;  
  11.   }  
  12.  
  13.   function jQuery(str)  
  14.   {  
  15.   return document.getElementById(str) ;  
  16.   }  
  17.   //--> 
  18.   </SCRIPT> 
  19.     <script src = "http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" language = "javascript"></script>   
  20.  </HEAD> 
  21.  
  22.  <BODY> 
  23.  <input type = "text" id = "txt1" value = "aa" /> 
  24.  </BODY> 
  25. </HTML> 

此處jQuery加載完畢已經(jīng)將第三方庫覆蓋了。如果想調(diào)用第三方庫,似乎有點(diǎn)困難。當(dāng)然jQuery已經(jīng)提供了解決方法,例4:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <HTML> 
  3.  <HEAD> 
  4.   <TITLE> New Document </TITLE> 
  5.     <SCRIPT LANGUAGE="JavaScript"> 
  6.   <!--  
  7.  //第三方庫  
  8.   function $(str)  
  9.   {  
  10.   return document.getElementById(str) ;  
  11.   }  
  12.  
  13.   function jQuery(str)  
  14.   {  
  15.   return document.getElementById(str) ;  
  16.   }  
  17.   //--> 
  18.   </SCRIPT> 
  19.     <script src = "http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" language = "javascript"></script>   
  20.  <script> 
  21.   //兼容代碼  
  22.   var $1 = $.noConflict();  
  23.   $1(document).ready(function(){  
  24.    alert($1("#txt1").val())  
  25.    alert($("txt1").value) ;  
  26.   })  
  27.  </script> 
  28.  </HEAD> 
  29.  
  30.  <BODY> 
  31.  <input type = "text" id = "txt1" value = "aa" /> 
  32.  </BODY> 
  33. </HTML> 

例4中同樣在jQuery載入之后調(diào)用"兼容代碼",和例2兼容代碼相同,但意義上有差別.在例2中第三方庫覆蓋了jQuery,其中兼容代碼的作用在第三方庫覆蓋jQuery前,jQuery入口指針賦給"$1".在例4中與上相反,由于jquery庫在載入完成時(shí),已經(jīng)將第三方庫覆蓋了,此時(shí)"$"指向jQuery庫,兼容代碼作用是將"$"重新指向第三方庫.同時(shí)充許重新定義jQuery入口.

jQuery兼容機(jī)制實(shí)現(xiàn)原理(示例代碼以jQuery-1.4.3為例):

  1. //29-32行  
  2. // Map over jQuery in case of overwrite  
  3. _jQuery = window.jQuery,  
  4.  
  5. // Map over the $ in case of overwrite  
  6. _$ = window.$,  
  7.    
  8. //394-402行  
  9.  noConflict: function( deep ) {  
  10.   window.$ = _$;  
  11.  
  12.   if ( deep ) {  
  13.    window.jQuery = _jQuery;  
  14.   }  
  15.  
  16.   return jQuery;  
  17.  }, 

其中29-32行,jQuery執(zhí)行前,將window.$,window.jQuery值保存到_$和_jQuery中(此時(shí)函數(shù)指針"jQuery","$"可能指向第三方庫,此處為兼容處理做準(zhǔn)備)。

394-402行將jQuery和$重新賦給window.$,window.jQuery,同時(shí)返回jQuery函數(shù)指針.  不難看出調(diào)用noConflict函數(shù)后,被jQuery"占用"的$與"jQuery"又交還給第三方庫了。

【編輯推薦】

  1. jQuery的運(yùn)行機(jī)制和設(shè)計(jì)理念
  2. jQuery開發(fā)者:你真的需要一個(gè)插件嗎?
  3. jQuery讓開發(fā)者戀戀不舍的秘密
  4. jQuery最佳實(shí)踐:精妙的自定義事件
  5. jQuery應(yīng)用程序性能指標(biāo)和調(diào)優(yōu)
責(zé)任編輯:王曉東 來源: 博客園
相關(guān)推薦

2011-08-15 17:20:25

iPhone應(yīng)用Sqlite3FMDB

2019-07-30 11:35:54

AndroidRetrofit

2015-11-05 16:44:37

第三方登陸android源碼

2012-03-01 20:42:12

iPhone

2011-08-05 16:50:00

iPhone 數(shù)據(jù) Sqlite

2014-08-06 10:15:06

Java 8

2014-07-22 10:56:45

Android Stu第三方類庫

2021-08-03 10:07:41

鴻蒙HarmonyOS應(yīng)用

2014-07-23 08:55:42

iOSFMDB

2011-08-16 18:46:35

IOS開發(fā)Three20緩存機(jī)制

2022-01-14 09:57:14

鴻蒙HarmonyOS應(yīng)用

2011-07-25 14:14:49

iPhone SQLITE Pldatabase

2010-03-03 15:10:49

第三方Python庫

2013-08-14 09:50:32

iOS類庫

2019-09-03 18:31:19

第三方支付電商支付行業(yè)

2021-10-11 06:38:52

Go開源庫語言

2009-12-31 14:38:34

Silverlight

2016-10-21 14:09:10

2017-12-11 15:53:56

2012-01-04 14:02:26

JsonCpp
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)