您的位置:首页 > Web前端 > JQuery

解决jQuery和其它库的冲突

2010-08-16 17:08 337 查看
在jQuery库中,几乎所有的插件都被限制在它的命名空间里。通常,全局(global)对象被很好地存储在jQuery命名空间里,因此当把jQuery和其他JavaScript库(例如Prototype、MooTools或YUI)一起使用时,不会引起冲突。

1.jQuery库在其他库这后导入
在其他库和jQuery库都被加载完毕后,可以在任何时候调用jQuery.noConflict()函数来将变量$的控制权移交给其它javaScript库。如:

jQuery先导入

<!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>冲突解决</title>
<!--先导入jQuery -->
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<!--后导入其他库 -->
<script src="js/prototype.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>

有了这些方法解决冲突,就可以放心在项目中引入jQuery了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: