標籤:style io color ar os 使用 java sp div
jQuery 名稱衝突
jQuery 使用 $ 符號作為 jQuery 的簡介方式。
某些其他 JavaScript 庫中的函數(比如 Prototype)同樣使用 $ 符號,這樣一起使用時就會起衝突!解決辦法如下。
一、使用jquery中的jQuery.noConflict(); 方法即可把變數$的控制權讓渡給第一個實現它的那個庫或之前自訂的$方法。之後應用Jquery的時候只要將所有的$換成jQuery即可,如原來引用對象方法$("#msg")改為jQuery("#msg")。如:<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
jQuery.noConflict();//開始使用jQuery(預設名)
jQuery(document).ready(function(){
jQuery("button").click(function(){
jQuery("p").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button type="button">Click me</button>
</body>
</html> 二、自訂jQuery使用符號 同樣用到jQuery 使用名為 noConflict() 的方法來解決該問題。var jq=jQuery.noConflict(),協助您使用自己的名稱(比如 jq)來代替 $ 符號。 如:<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
var jq=jQuery.noConflict();
jq(document).ready(function(){
jq("button").click(function(){
jq("p").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button type="button">Click me</button>
</body>
</html>
jQuery 名稱衝突