繁体   English   中英

jQuery和Mootools,.noConflict失败

[英]Jquery And Mootools, .noConflict is failing

我有一个需要使用mootools日历的应用程序,但是,我将jquery用于我的主要应用程序结构。

一旦我将mootools与jquery一起使用,它们都无法工作,而当我只有一个时,它们就可以正常工作。 我进行了一些研究,说我可以使用一种称为.noconflict的方法,尽管尝试了一下,但仍未解决问题。 也许有人可以更好地向我解释在哪里进行实际的.noconflict调用,也许可以帮助我完成此工作。

谢谢!

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var fixHeight = function () {
                var gutter = 50;
                var newHeight = $(window).height() - (gutter*2);
                $('#container').height(newHeight);
        }
        $(function(){ 

            fixHeight();
            $('ul#nav li.item').each(function(index) {
                if ($(this).attr("name") == "media") {
                    $(this).attr("id","active");
                }
            });
            $('input').focus(function() {
                $(this).attr("value","");
            }).blur(function() {
                $(this).attr("value",$(this).attr("original-value"));
            });

            $(window).resize(function() {
                fixHeight();
            }).load(function() {
                fixHeight();
            });

        });  
     </script>
     <script type="text/javascript" src="http://taptouchclick.com/js/mootools-1.2.4-core-yc.js"></script>
     <script type="text/javascript">
        window.addEvent('domready', function(){
             var smoothCalendar = new SmoothCalendar("calendar");
        });
    </script>

是的, noConflict方法应该可以工作,但是如果无效,或者您想以其他方式执行,则应将脚本封装在自调用函数中,并将参数设置为主库对象:

(function($){
    // al your jquery logic here
    alert($ instanceof jQuery);
})(jQuery)

之后,您应该包括下一个库(在您的情况下为MooTools)并正常编写脚本,或者-可以肯定的是-您也可以将MooTools逻辑封装在一个函数中。

如果您简单地致电:

jQuery.noConflict();

...包括Mootools JS文件之前。

然后,您可以使用jQuery而不是$来使用jQuery函数。 例:

jQuery('.selector').hide();  // instead of $('.selector').hide();

您还可以通过DOM就绪事件将$传递回:

$.noConflict();

// Non-jQuery code goes here

jQuery(document).ready(function($) {
    // You can now use the dollar sign safely inside of this function
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM