jQuery 使用 $ 符号作为 jQuery 的简介方式。
某些其他 JavaScript 库中的函数(比如 Prototype)同样使用 $ 符号。
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与其他JavaScript库共存时避免$符号冲突。通过使用jQuery提供的noConflict()方法,可以将jQuery的引用替换为其他名称,从而允许页面上同时运行多个库。
267

被折叠的 条评论
为什么被折叠?



