欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery多個(gè)版本和其他js庫沖突的解決方法

 更新時(shí)間:2016年08月11日 11:24:41   作者:阿訊小飛  
jQuery多個(gè)版本或和其他js庫沖突主要是常用的$符號(hào)的問題,該怎么解決呢?下面小編給大家?guī)砹薺Query多個(gè)版本和其他js庫沖突的解決方法,一起看下吧

jQuery多個(gè)版本或和其他js庫沖突主要是常用的$符號(hào)的問題,這個(gè)問題 jquery早早就有給我們預(yù)留處理方法了,下面一起來看看解決辦法。

1.同一頁面jQuery多個(gè)版本或沖突解決方法。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery測試頁</title>
</head>
<body>
<!-- 引入1.6.4版的jq -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js"></script>
<script> var jq164 = jQuery.noConflict(true); </script>
<!-- 引入1.4.2版的jq -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script> var jq142 = jQuery.noConflict(true); </script>
<script>
(function($){
//此時(shí)的$是jQuery-1.6.4
$('#');
})(jq164);
</script>
<script>
jq142(function($){
//此時(shí)的$是jQuery-1.4.2
$('#');
});
</script>
</body>
</html>

2.同一頁面jQuery和其他js庫沖突解決方法

jQuery noConflict() 方法

noConflict() 方法會(huì)釋放會(huì) $ 標(biāo)識(shí)符的控制,這樣其他腳本就可以使用它了。

jquery.js在prototype.js之前進(jìn)行引入,如:

<script src="jquery.js" type="text/javascript"></script>
<script src="prototype.js" type="text/javascript"></script>
<p id="pp">test---prototype</p>
<p>test---jQuery</p> 

2.1 當(dāng)然,您仍然可以通過全名替代簡寫的方式來使用 jQuery:

<script type="text/javascript">
jQuery.noConflict(); //將變量$的控制權(quán)讓渡給prototype.js,全名可以不調(diào)用。
jQuery(function(){ //使用jQuery
jQuery("p").click(function(){
alert( jQuery(this).text() );
});
});
//此處不可以再寫成$,此時(shí)的$代表prototype.js中定義的$符號(hào)。
$("pp").style.display = 'none'; //使用prototype
</script> 

2.2 您也可以創(chuàng)建自己的簡寫。noConflict() 可返回對(duì) jQuery 的引用,您可以把它存入變量,以供稍后使用。請(qǐng)看這個(gè)例子:

<script type="text/javascript">
var $j = jQuery.noConflict(); //自定義一個(gè)比較短快捷方式
$j(function(){ //使用jQuery
$j("p").click(function(){
alert( $j(this).text() );
});
});
$("pp").style.display = 'none'; //使用prototype
</script> 

2.3 如果你的 jQuery 代碼塊使用 $ 簡寫,并且您不愿意改變這個(gè)快捷方式,那么您可以把 $ 符號(hào)作為變量傳遞給 ready 方法。這樣就可以在函數(shù)內(nèi)使用 $ 符號(hào)了 - 而在函數(shù)外,依舊不得不使用 "jQuery":

<script type="text/javascript">
jQuery.noConflict(); //將變量$的控制權(quán)讓渡給prototype.js
jQuery(document).ready(function($){
$("p").click(function(){ //繼續(xù)使用 $ 方法
alert( $(this).text() );
});
});
jQuery(function($){ //使用jQuery
$("p").click(function(){ //繼續(xù)使用 $ 方法
alert( $(this).text() );
});
});
</script> 

2.4 使用語句塊:

<script type="text/javascript">
jQuery.noConflict(); //將變量$的控制權(quán)讓渡給prototype.js
(function($){ //定義匿名函數(shù)并設(shè)置形參為$
$(function(){ //匿名函數(shù)內(nèi)部的$均為jQuery
$("p").click(function(){ //繼續(xù)使用 $ 方法
alert($(this).text());
});
});
})(jQuery); //執(zhí)行匿名函數(shù)且傳遞實(shí)參jQuery
$("pp").style.display = 'none'; //使用prototype
</script> 

這種使用語句塊的方法非常有用,在我們自己寫jquery插件時(shí),應(yīng)該都使用這種寫法,因?yàn)槲覀儾恢谰唧w工作過程中是如何順序引入各種js庫的,而這種語句塊的寫法卻能屏蔽沖突。

注意:

1.引用javascript類庫時(shí),把jQuery引用放在最后面,可以避免沖突。

2.特別要注意jQuery()代替$()時(shí),jQuery是區(qū)分大小寫的,因?yàn)閖avascript本身就是區(qū)分大小寫的。

以上所述是小編給大家介紹的jQuery多個(gè)版本和其他js庫沖突的解決方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論