如何解决jQuery和mootoools冲突?
我用来
<script type="text/javascript" src="jquery-1.2.2.pack.js"> </script>
加载 jquery,然后加载包含这些的外部脚本:
var jkpanel={
controltext: 'menu',
$mainpanel: null, contentdivheight: 0,
openclose:function($, speed){
this.$mainpanel.stop() //stop any animation
if (this.$mainpanel.attr('openstate')=='closed')
this.$mainpanel.animate({top: 0}, speed).attr({openstate: 'open'})
else
this.$mainpanel.animate({top: -this.contentdivheight+'px'}, speed).attr({openstate: 'closed'})
},
init:function(file, height, speed){
jQuery(document).ready(function($){
jkpanel.$mainpanel=$('<div id="dropdownpanel"><div class="contentdiv"></div><div class="control">'+jkpanel.controltext+'</div></div>').prependTo('body')
var $contentdiv=jkpanel.$mainpanel.find('.contentdiv')
var $controldiv=jkpanel.$mainpanel.find('.control').css({cursor: 'wait'})
$contentdiv.load(file, '', function($){
var heightattr=isNaN(parseInt(height))? 'auto' : parseInt(height)+'px'
$contentdiv.css({height: heightattr})
jkpanel.contentdivheight=parseInt($contentdiv.get(0).offsetHeight)
jkpanel.$mainpanel.css({top:-jkpanel.contentdivheight+'px', visibility:'visible'}).attr('openstate', 'closed')
$controldiv.css({cursor:'hand', cursor:'pointer'})
})
jkpanel.$mainpanel.click(function(){jkpanel.openclose($, speed)})
})
}
}
//Initialize script: jkpanel.init('path_to_content_file', 'height of content DIV in px', animation_duration)
jkpanel.init('1', '80px', 1000)
当然也使用 mootools 插件。
我的问题是我应该如何在上面的脚本中使用var $j = jQuery.noConflict();
来防止冲突
I use
<script type="text/javascript" src="jquery-1.2.2.pack.js"> </script>
to load jquery and then load an external script that contains these :
var jkpanel={
controltext: 'menu',
$mainpanel: null, contentdivheight: 0,
openclose:function($, speed){
this.$mainpanel.stop() //stop any animation
if (this.$mainpanel.attr('openstate')=='closed')
this.$mainpanel.animate({top: 0}, speed).attr({openstate: 'open'})
else
this.$mainpanel.animate({top: -this.contentdivheight+'px'}, speed).attr({openstate: 'closed'})
},
init:function(file, height, speed){
jQuery(document).ready(function($){
jkpanel.$mainpanel=$('<div id="dropdownpanel"><div class="contentdiv"></div><div class="control">'+jkpanel.controltext+'</div></div>').prependTo('body')
var $contentdiv=jkpanel.$mainpanel.find('.contentdiv')
var $controldiv=jkpanel.$mainpanel.find('.control').css({cursor: 'wait'})
$contentdiv.load(file, '', function($){
var heightattr=isNaN(parseInt(height))? 'auto' : parseInt(height)+'px'
$contentdiv.css({height: heightattr})
jkpanel.contentdivheight=parseInt($contentdiv.get(0).offsetHeight)
jkpanel.$mainpanel.css({top:-jkpanel.contentdivheight+'px', visibility:'visible'}).attr('openstate', 'closed')
$controldiv.css({cursor:'hand', cursor:'pointer'})
})
jkpanel.$mainpanel.click(function(){jkpanel.openclose($, speed)})
})
}
}
//Initialize script: jkpanel.init('path_to_content_file', 'height of content DIV in px', animation_duration)
jkpanel.init('1', '80px', 1000)
and also use a mootools plugin of course.
MY QUESTION IS THAT how should I use var $j = jQuery.noConflict();
in the above script to prevent conflicting
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果问题只是,您加载 MooTools,然后加载 jQuery,然后 MooTools 不起作用,因为 jQuery 已经接管了美元功能,那么您可能只需要这样的代码:
That should get jQuery to relinquish
$()
。您问题中的代码已经做了其他方便的事情,即使用就绪事件处理程序的参数作为在本地使用jQuery
对象的较短名称的方法。我强烈建议阅读 有关使用其他库的 jQuery 页面,也许还有
noConflict()
函数。If the problem is just, you load MooTools, and then you load jQuery, and then MooTools doesn't work because jQuery has taken over the dollar function, then you probably just need code like this:
That should get jQuery to relinquish
$()
. The code you have in your question already does the other handy thing, which is use the parameter to the ready event handler as a way to locally use a shorter name for thejQuery
object.I'd strongly recommend reading the jQuery page on working with other libraries and maybe the documentation for the
noConflict()
function.将所有依赖 jQuery 的 JavaScript 封装在一个闭包中,以防止命名空间冲突,如下所示:
看起来很奇怪,但语法是正确的(是的,第一行以分号开头)。这会自动用
jQuery
替换$
对象,jQuery 和 mootools 都使用该对象。由于您同时使用两者,因此您应该将所有 jQuery 代码包装在像这样的闭包中(每个 .js 文件或script
标记一个)。Wrap all the JavaScript that relies on jQuery in a closure to prevent namespace conflicts, like so:
It looks weird, but the syntax is right (yes, the first line starts with a semicolon). This automatically substitutes
jQuery
for the$
object, which both jQuery and mootools make use of. Since you're using both, you should wrap all of your jQuery code in a closure like this (one for each .js file orscript
tag).