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

Jquery命名沖突解決的五種方案分享

 更新時(shí)間:2012年03月16日 23:04:54   作者:  
最近遇到個(gè)問(wèn)題,同時(shí)引用了jquery庫(kù)和另外一個(gè)js庫(kù)。當(dāng)用$XX去調(diào)用js庫(kù)函數(shù)時(shí),發(fā)現(xiàn)失效了!于是找資料,原來(lái)是jquery命名沖突了
因?yàn)樵S多 JavaScript 庫(kù)使用 $ 作為函數(shù)或變量名,jquery也一樣。其實(shí)$只是jquery的一個(gè)別名而已,假如我們需要使用 jquery 之外的另一 js 庫(kù),我們可以通過(guò)調(diào)用 $.noConflict() 向該庫(kù)返回控制權(quán)。下面是收集到解決這一問(wèn)題的五種方案,總有一種你會(huì)用得上的。
例一:
復(fù)制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>沖突解決1</title>
<!-- 引入 prototype -->
<script src="prototype-1.6.0.3.js" type="text/javascript"></script>
<!-- 引入 jQuery -->
<script src="http://www.cnblogs.com/scripts/jquery-1.3.1.js" type="text/javascript"></script>
</head>
<body>
<p id="pp">test---prototype</p>
<p >test---jQuery</p>
<script type="text/javascript">
jQuery.noConflict(); //將變量$的控制權(quán)讓渡給prototype.js
jQuery(function(){ //使用jQuery
jQuery("p").click(function(){
alert( jQuery(this).text() );
});
});
$("pp").style.display = 'none'; //使用prototype
</script>
</body>
</html>

例二:
復(fù)制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>沖突解決2</title>
<!-- 引入 prototype -->
<script src="prototype-1.6.0.3.js" type="text/javascript"></script>
<!-- 引入 jQuery -->
<script src="http://www.cnblogs.com/scripts/jquery-1.3.1.js" type="text/javascript"></script>
</head>
<body>
<p id="pp">test---prototype</p>
<p >test---jQuery</p>
<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>
</body>
</html>

例三:
復(fù)制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>沖突解決3</title>
<!-- 引入 prototype -->
<script src="prototype-1.6.0.3.js" type="text/javascript"></script>
<!-- 引入 jQuery -->
<script src="http://www.cnblogs.com/scripts/jquery-1.3.1.js" type="text/javascript"></script>
</head>
<body>
<p id="pp">test---prototype</p>
<p >test---jQuery</p>
<script type="text/javascript">
jQuery.noConflict(); //將變量$的控制權(quán)讓渡給prototype.js
jQuery(function($){ //使用jQuery
$("p").click(function(){ //繼續(xù)使用 $ 方法
alert( $(this).text() );
});
});
$("pp").style.display = 'none'; //使用prototype
</script>
</body>
</html>

例四:
復(fù)制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>沖突解決4</title>
<!-- 引入 prototype -->
<script src="prototype-1.6.0.3.js" type="text/javascript"></script>
<!-- 引入 jQuery -->
<script src="http://www.cnblogs.com/scripts/jquery-1.3.1.js" type="text/javascript"></script>
</head>
<body>
<p id="pp">test---prototype</p>
<p >test---jQuery</p>
<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>
</body>
</html>

例五:
復(fù)制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>沖突解決5</title>
<!--先導(dǎo)入jQuery -->
<script src="http://www.cnblogs.com/scripts/jquery-1.3.1.js" type="text/javascript"></script>
<!--后導(dǎo)入其他庫(kù) -->
<script src="prototype-1.6.0.3.js" type="text/javascript"></script>
</head>
<body>
<p id="pp">test---prototype</p>
<p >test---jQuery</p>
<script type="text/javascript">
jQuery(function(){ //直接使用 jQuery ,沒(méi)有必要調(diào)用"jQuery.noConflict()"函數(shù)。
jQuery("p").click(function(){
alert( jQuery(this).text() );
});
});
$("pp").style.display = 'none'; //使用prototype
</script>
</body>
</html>

上述實(shí)例打包下載

相關(guān)文章

  • jquery 驗(yàn)證用戶名是否重復(fù)代碼實(shí)例

    jquery 驗(yàn)證用戶名是否重復(fù)代碼實(shí)例

    這篇文章主要介紹了jquery驗(yàn)證用戶名是否重復(fù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • jQuery 文本框模擬下拉列表效果

    jQuery 文本框模擬下拉列表效果

    學(xué)校作業(yè)的問(wèn)題,就在網(wǎng)搜了一下,找不到文本框模擬的(可能本人RP問(wèn)題),看到的都是用div的,結(jié)果就自己弄了一個(gè),主要就是改變背景圖片的位置,讓div的位置放到文本框上面,沒(méi)什么其它的東西,呵呵,見(jiàn)笑了
    2010-02-02
  • jquery UI實(shí)現(xiàn)autocomplete在獲取焦點(diǎn)時(shí)得到顯示列表功能示例

    jquery UI實(shí)現(xiàn)autocomplete在獲取焦點(diǎn)時(shí)得到顯示列表功能示例

    這篇文章主要介紹了jquery UI實(shí)現(xiàn)autocomplete在獲取焦點(diǎn)時(shí)得到顯示列表功能,結(jié)合實(shí)例形式分析了jquery UI實(shí)現(xiàn)autocomplete事件響應(yīng)及顯示下拉列表功能操作技巧,需要的朋友可以參考下
    2019-06-06
  • 詳談jQuery Ajax(load,post,get,ajax)的用法

    詳談jQuery Ajax(load,post,get,ajax)的用法

    下面小編就為大家?guī)?lái)一篇詳談jQuery Ajax(load,post,get,ajax)的用法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03
  • jQuery 淡入/淡出效果函數(shù)用法分析

    jQuery 淡入/淡出效果函數(shù)用法分析

    這篇文章主要介紹了jQuery 淡入/淡出效果函數(shù)用法,結(jié)合實(shí)例形式分析了jQuery 淡入/淡出功能fadeIn()、fadeOut()、fadeToggle()及fadeTo()函數(shù)基本功能、使用方法與操作注意事項(xiàng),需要的朋友可以參考下
    2020-05-05
  • jquery實(shí)現(xiàn)自定義樹(shù)形表格的方法【自定義樹(shù)形結(jié)構(gòu)table】

    jquery實(shí)現(xiàn)自定義樹(shù)形表格的方法【自定義樹(shù)形結(jié)構(gòu)table】

    這篇文章主要介紹了jquery實(shí)現(xiàn)自定義樹(shù)形表格的方法,結(jié)合實(shí)例形式分析了jQuery創(chuàng)建自定義樹(shù)形結(jié)構(gòu)table的相關(guān)操作技巧,需要的朋友可以參考下
    2019-07-07
  • Web開(kāi)發(fā)者必備的12款超贊jQuery插件

    Web開(kāi)發(fā)者必備的12款超贊jQuery插件

    jQuery插件能夠增強(qiáng)網(wǎng)站的可用性,有效地改善用戶體驗(yàn),還可以大大減少創(chuàng)建一個(gè)新站點(diǎn)的開(kāi)發(fā)時(shí)間。
    2010-12-12
  • jquery1.4 教程一 便利的設(shè)置函數(shù)

    jquery1.4 教程一 便利的設(shè)置函數(shù)

    jquery1.4已經(jīng)發(fā)布了,1.4相對(duì)于1.32的改進(jìn)還是非常巨大的,可以說(shuō)是全面性的,性能也全面超越了1.32。這一周,將一一展示jquery1.4的改進(jìn),同時(shí)也會(huì)放出相應(yīng)的demo。
    2010-02-02
  • jquery uploadify隱藏上傳進(jìn)度的實(shí)現(xiàn)方法

    jquery uploadify隱藏上傳進(jìn)度的實(shí)現(xiàn)方法

    下面小編就為大家?guī)?lái)一篇jquery uploadify隱藏上傳進(jìn)度的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • jquery.pager.js實(shí)現(xiàn)分頁(yè)效果

    jquery.pager.js實(shí)現(xiàn)分頁(yè)效果

    這篇文章主要為大家詳細(xì)介紹了jquery.pager.js實(shí)現(xiàn)分頁(yè)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07

最新評(píng)論