如何正确实现 jQuery noConflict() 方法?
我无法让 noConflict()
方法正常工作。我在网上搜索了几个小时,似乎无法得到可靠的答案。这是我的标题中的内容...
<script type="text/javascript" src="js/mootools.js"></script>
<script type="text/javascript" src="js/slimbox.js"></script>
<script type="text/javascript" src="js/jquery.js"></script><!-- jQuery script -->
<script type="text/javascript" src="js/page_scroll.js"></script><!-- JavaScript -->
<script src="js/jquery.easing.1.3.js" type="text/javascript"></script><!-- jQuery easing plug-in -->
<script type="text/javascript">
var $j = jQuery.noConflict();
// Use jQuery via $j(...)
$j(document).ready(function(){
$j("div").hide();
});
// Use Mootools with $(...), etc.
$('someid').hide();
</script>
我的 HTML 中没有其他 jQuery 或 JavaScript(只是我在外部拥有的),因此“将任何使用 $
变量的内容放在 noConlict< /code> 方法”不是一个选项。
本质上我想做的是让 slimbox 和缓动页面滚动(用于我的导航)一起工作。问题当然是,当另一个被移除时,一个可以工作,反之亦然。另外,当我使用 noConflict()
方法时,我让 slimbox 工作,而不是 pagescroll,但是当我删除 noConflict()
方法时,我让 pagescroll 工作,而不是 slimbox 。所以显然 noConflict
正在做一些事情,我只是不知道那是什么或如何去做。
如果有人能帮助我,我将不胜感激。谢谢。
I can't get the noConflict()
method to work properly. I've been searching online for hours and can't seem to get a solid answer. Here's what I have in my header...
<script type="text/javascript" src="js/mootools.js"></script>
<script type="text/javascript" src="js/slimbox.js"></script>
<script type="text/javascript" src="js/jquery.js"></script><!-- jQuery script -->
<script type="text/javascript" src="js/page_scroll.js"></script><!-- JavaScript -->
<script src="js/jquery.easing.1.3.js" type="text/javascript"></script><!-- jQuery easing plug-in -->
<script type="text/javascript">
var $j = jQuery.noConflict();
// Use jQuery via $j(...)
$j(document).ready(function(){
$j("div").hide();
});
// Use Mootools with $(...), etc.
$('someid').hide();
</script>
There's no other jQuery or JavaScript within my HTML (just what I have externally), so "placing anything that uses the $
variable in the noConlict
method" is not an option.
Essentially what I'm trying to do is have slimbox and easing page scroll (for my navigation) to work together. The problem is of course one works when the other is removed and vice versa. Also, when I use the noConflict()
method I get slimbox to work and not pagescroll, but when I remove the noConflict()
method I get pagescroll to work and not slimbox. So apperently noConflict
is doing something, I just don't know what that is or how to go about it.
If anyone can help me out, I'd appreciate it. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当使用其他利用 $ 别名的 js 库时,
我通过使用找到了可靠的结果
when using other js libs that leverage the $ alias,
I have found reliable results by using
那么你是说声明 $j 时它不起作用吗?您可以定义自己的备用名称(例如 jq、$J、awesomeQuery - 任何您想要的名称)。尝试将其称为不带 $ 的名称
So are you saying it does not work when you declare $j? You can define your own alternate names (e.g. jq, $J, awesomeQuery - anything you want). Try calling it something w/o the $
将其包装在匿名函数中:
查看确切的代码可能会有所帮助。
Wrap it in an anonymous function:
Seeing the exact code might help.