`

JQuery选取器与其它JS框架冲突的解决方法 确保jQuery不会与其他库的$对象发生冲突

阅读更多

 

隆重推荐:

jQuery.noConflict()方法

 

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->运行这个函数将变量$的控制权让渡给第一个实现它的那个库。
这有助于确保jQuery不会与其他库的$对象发生冲突。

在运行这个函数后,就只能使用jQuery变量访问jQuery对象。例如,在要用到$("div p")的地方,就必须换成jQuery("div p")。

注意:这个函数必须在你导入jQuery文件之后,并且在导入另一个导致冲突的库之前使用。当然也应当在其他冲突的库被使用之前,除非jQuery是最后一个导入的。

 

使用方式1:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->jQuery.noConflict();
// 使用 jQuery
jQuery("div p").hide(); 
// 使用其他库的 $() 
$("content").style.display = 'none'

 

使用方式2:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->//我的一个站点 viqiwu.com
var viqiwu = jQuery.noConflict();
// 基于 jQuery 的代码
viqiwu("div p").hide(); 
// 基于其他库的 $() 代码 
$("content").style.display = 'none';

 

这样就不用因为JQuery和其它的JS框架有冲突,而犯愁了。代码我在jquery-1.2.6下测试通过。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics