jQuery学习之六-----重命名jQuery库

jQuery 名称冲突

jQuery 使用 $ 符号作为 jQuery 的简介方式。

某些其他 JavaScript 库中的函数(比如 Prototype)同样使用 $ 符号。

jQuery 使用名为 noConflict() 的方法来解决该问题。

var jq=jQuery.noConflict(),帮助您使用自己的名称(比如 jq)来代替 $ 符号。

 1 <html>
 2 <head>
 3 <script type="text/javascript" src="/jquery/jquery.js"></script>
 4 <script type="text/javascript">
 5 var jq=jQuery.noConflict();
 6 jq(document).ready(function(){
 7   jq("button").click(function(){
 8     jq("p").hide();
 9   });
10 });
11 </script>
12 </head>
13 
14 <body>
15 <h2>This is a heading</h2>
16 <p>This is a paragraph.</p>
17 <p>This is another paragraph.</p>
18 <button type="button">Click me</button>
19 </body>
20 </html>