jQuery $命名衝突解決方案匯總,jquery解決方案

來源:互聯網
上載者:User

jQuery $命名衝突解決方案匯總,jquery解決方案

最近遇到個問題,同時引用了jquery庫和另外一個js庫。當用$XX去調用js庫函數時,發現失效了!於是找資料,原來是jquery命名衝突了。因為許多JavaScript 庫使用$作為函數或變數名,jquery也一樣。其實$只是jquery的一個別名而已,假如我們需要使用jquery 之外的另一js庫,我們可以通過調用 $.noConflict() 向該庫返回控制權。下面是收集到解決這一問題的五種方案,總有一種你會用得上的。

例一:

複製代碼 代碼如下:
<!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="/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();                //將變數$的控制權讓渡給prototype.js 
jQuery(function(){                    //使用jQuery 
    jQuery("p").click(function(){ 
        alert( jQuery(this).text() ); 
    }); 
}); 
$("pp").style.display = 'none';        //使用prototype 
</script>
</body>
</html>

例二:

複製代碼 代碼如下:
<!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="/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();        //自訂一個比較短捷徑 
$j(function(){                        //使用jQuery 
    $j("p").click(function(){ 
        alert( $j(this).text() ); 
    }); 
}); 
$("pp").style.display = 'none';        //使用prototype 
</script>
</body>
</html>

例三:

複製代碼 代碼如下:
<!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="/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();                //將變數$的控制權讓渡給prototype.js 
jQuery(function($){                    //使用jQuery 
    $("p").click(function(){        //繼續使用 $ 方法 
        alert( $(this).text() ); 
    }); 
}); 
$("pp").style.display = 'none';        //使用prototype 
</script>
</body>
</html>

例四:

複製代碼 代碼如下:
<!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="/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();                //將變數$的控制權讓渡給prototype.js 
(function($){                        //定義匿名函數並設定形參為$ 
    $(function(){                    //匿名函數內部的$均為jQuery 
        $("p").click(function(){    //繼續使用 $ 方法 
            alert($(this).text()); 
        }); 
    }); 
})(jQuery);                            //執行匿名函數且傳遞實參jQuery 
$("pp").style.display = 'none';        //使用prototype 
</script>
</body>
</html>

例五:

複製代碼 代碼如下:
<!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>
<!--先匯入jQuery -->
<script src="/scripts/jquery-1.3.1.js" type="text/javascript"></script>
<!--後匯入其他庫 -->
<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 ,沒有必要調用"jQuery.noConflict()"函數。 
    jQuery("p").click(function(){       
        alert( jQuery(this).text() ); 
    }); 
}); 
$("pp").style.display = 'none'; //使用prototype 
</script>
</body>
</html>

看,原來解決問題的方法可不止一個兩個呢,有需要的小夥伴們自己來取吧

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.