當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
jQuery库与其他JS库冲突的解决办法
生活随笔
收集整理的這篇文章主要介紹了
jQuery库与其他JS库冲突的解决办法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
關鍵字: javascript
現在的js庫很多,而且各有所長。像我,就比較喜歡jQuery,但同時也會使用一下其他的js庫,如YUI,DWR什么的。但是它們卻時不時地相互鬧些小矛盾,真是讓人頭痛。究其原因,原來是它們的全局對象定義沖突了,特別是變量”$”,幾乎百分之百要在這中招。怎么辦?呵呵,看我的!
重載$函數。使用jQuery.noConflict()就可以通過重載$函數,從而區分開跟其他js庫的重疊部分。例子如下:
Js代碼
<script src="prototype.js"></script>
<script src="<SPAN class=hilite1>jquery</SPAN>.js"></script>
<script>
<SPAN class=hilite1>jQuery</SPAN>.noConflict();
// Use <SPAN class=hilite1>jQuery</SPAN> via <SPAN class=hilite1>jQuery</SPAN>(…)
<SPAN class=hilite1>jQuery</SPAN>(document).ready(function(){
<SPAN class=hilite1>jQuery</SPAN>(”div”).hide();
});
// Use Prototype with $(…), etc.
$(’someid’).style.display = ‘none’;
</script>
<script src="prototype.js"></script>
<script src="jquery.js"></script>
<script>
jQuery.noConflict();
// Use jQuery via jQuery(…)
jQuery(document).ready(function(){
jQuery(”div”).hide();
});
// Use Prototype with $(…), etc.
$(’someid’).style.display = ‘none’;
</script>
記得要在載入完各js庫后才能用jQuery.noConflict()進行重載哦,如例子中的prototype和jquery。
當然我們也可以不使用其默認的名字空間,比如我喜歡使用 $j() 而不是 jQuery() 。
Js代碼
var $j = <SPAN class=hilite1>jQuery</SPAN>.noConflict();
$j(document).ready(function(){
$j(”div”).hide();
});
var $j = jQuery.noConflict();
$j(document).ready(function(){
$j(”div”).hide();
});
呵呵,很簡單吧。原文在這里http://docs.jquery.com/Using_jQuery_with_Other_Libraries
現在的js庫很多,而且各有所長。像我,就比較喜歡jQuery,但同時也會使用一下其他的js庫,如YUI,DWR什么的。但是它們卻時不時地相互鬧些小矛盾,真是讓人頭痛。究其原因,原來是它們的全局對象定義沖突了,特別是變量”$”,幾乎百分之百要在這中招。怎么辦?呵呵,看我的!
重載$函數。使用jQuery.noConflict()就可以通過重載$函數,從而區分開跟其他js庫的重疊部分。例子如下:
Js代碼
<script src="prototype.js"></script>
<script src="<SPAN class=hilite1>jquery</SPAN>.js"></script>
<script>
<SPAN class=hilite1>jQuery</SPAN>.noConflict();
// Use <SPAN class=hilite1>jQuery</SPAN> via <SPAN class=hilite1>jQuery</SPAN>(…)
<SPAN class=hilite1>jQuery</SPAN>(document).ready(function(){
<SPAN class=hilite1>jQuery</SPAN>(”div”).hide();
});
// Use Prototype with $(…), etc.
$(’someid’).style.display = ‘none’;
</script>
<script src="prototype.js"></script>
<script src="jquery.js"></script>
<script>
jQuery.noConflict();
// Use jQuery via jQuery(…)
jQuery(document).ready(function(){
jQuery(”div”).hide();
});
// Use Prototype with $(…), etc.
$(’someid’).style.display = ‘none’;
</script>
記得要在載入完各js庫后才能用jQuery.noConflict()進行重載哦,如例子中的prototype和jquery。
當然我們也可以不使用其默認的名字空間,比如我喜歡使用 $j() 而不是 jQuery() 。
Js代碼
var $j = <SPAN class=hilite1>jQuery</SPAN>.noConflict();
$j(document).ready(function(){
$j(”div”).hide();
});
var $j = jQuery.noConflict();
$j(document).ready(function(){
$j(”div”).hide();
});
呵呵,很簡單吧。原文在這里http://docs.jquery.com/Using_jQuery_with_Other_Libraries
總結
以上是生活随笔為你收集整理的jQuery库与其他JS库冲突的解决办法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OPPO K10、一加 Ace 竞速版开
- 下一篇: 本金和佣金是什么意思