如何正确实现 jQuery noConflict() 方法?

发布于 2024-12-17 13:33:26 字数 1251 浏览 5 评论 0原文

我无法让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里兽 2024-12-24 13:33:26

当使用其他利用 $ 别名的 js 库时,
我通过使用找到了可靠的结果

$.noConflict();

jQuery(document).ready(function($) {

  // your jQuery code here i.e. 

  // jQuery('some-selector-expression').somefunction();

});

// prototype or mootools code that uses the $ alias works fine here

when using other js libs that leverage the $ alias,
I have found reliable results by using

$.noConflict();

jQuery(document).ready(function($) {

  // your jQuery code here i.e. 

  // jQuery('some-selector-expression').somefunction();

});

// prototype or mootools code that uses the $ alias works fine here
む无字情书 2024-12-24 13:33:26

那么你是说声明 $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 $

小兔几 2024-12-24 13:33:26

将其包装在匿名函数中:

(function($){
    $(document).ready(function() {
        // function here...
    });
})(jQuery);

查看确切的代码可能会有所帮助。

Wrap it in an anonymous function:

(function($){
    $(document).ready(function() {
        // function here...
    });
})(jQuery);

Seeing the exact code might help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文