当前位置: 移动技术网 > IT编程>开发语言>JavaScript > [jQuery]名称冲突使用noConflict方法解决

[jQuery]名称冲突使用noConflict方法解决

2019年01月15日  | 移动技术网IT编程  | 我要评论

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>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网