0

我正在向我的 magento 站点添加一个脚本,并且由于该脚本使用不同版本的 JS,因此出现原型错误

我试着做

$.noConflict(); // 使用其他库的 $ 的代码可以在这里跟随。

没有成功

没有冲突的最佳方法是什么?

<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
<script>
$(function() {
    $( "#accordion" ).accordion({
        event: "click hoverintent"
    });
});

var cfg = ($.hoverintent = {
    sensitivity: 7,
    interval: 100
});

$.event.special.hoverintent = {
    setup: function() {
        $( this ).bind( "mouseover", jQuery.event.special.hoverintent.handler );
    },
    teardown: function() {
        $( this ).unbind( "mouseover", jQuery.event.special.hoverintent.handler );
    },
    handler: function( event ) {
        var that = this,
            args = arguments,
            target = $( event.target ),
            cX, cY, pX, pY;

        function track( event ) {
            cX = event.pageX;
            cY = event.pageY;
        };
        pX = event.pageX;
        pY = event.pageY;
        function clear() {
            target
                .unbind( "mousemove", track )
                .unbind( "mouseout", arguments.callee );
            clearTimeout( timeout );
        }
        function handler() {
            if ( ( Math.abs( pX - cX ) + Math.abs( pY - cY ) ) < cfg.sensitivity ) {
                clear();
                event.type = "hoverintent";
                // prevent accessing the original event since the new event
                // is fired asynchronously and the old event is no longer
                // usable (#6028)
                event.originalEvent = {};
                jQuery.event.handle.apply( that, args );
            } else {
                pX = cX;
                pY = cY;
                timeout = setTimeout( handler, cfg.interval );
            }
        }
        var timeout = setTimeout( handler, cfg.interval );
        target.mousemove( track ).mouseout( clear );
        return true;
    }
};
</script>
4

1 回答 1

0

如果您只是在此匿名函数中使用 jQuery,则可以将所有内容包装在匿名函数中:

(function ($) {
    $( "#accordion" ).accordion({
        event: "click hoverintent"
    });


var cfg = ($.hoverintent = {
    sensitivity: 7,
    interval: 100
});

$.event.special.hoverintent = {
    setup: function() {
        $( this ).bind( "mouseover", jQuery.event.special.hoverintent.handler );
    },
    teardown: function() {
        $( this ).unbind( "mouseover", jQuery.event.special.hoverintent.handler );
    },
    handler: function( event ) {
        var that = this,
            args = arguments,
            target = $( event.target ),
            cX, cY, pX, pY;

        function track( event ) {
            cX = event.pageX;
            cY = event.pageY;
        };
        pX = event.pageX;
        pY = event.pageY;
        function clear() {
            target
                .unbind( "mousemove", track )
                .unbind( "mouseout", arguments.callee );
            clearTimeout( timeout );
        }
        function handler() {
            if ( ( Math.abs( pX - cX ) + Math.abs( pY - cY ) ) < cfg.sensitivity ) {
                clear();
                event.type = "hoverintent";
                // prevent accessing the original event since the new event
                // is fired asynchronously and the old event is no longer
                // usable (#6028)
                event.originalEvent = {};
                jQuery.event.handle.apply( that, args );
            } else {
                pX = cX;
                pY = cY;
                timeout = setTimeout( handler, cfg.interval );
            }
        }
        var timeout = setTimeout( handler, cfg.interval );
        target.mousemove( track ).mouseout( clear );
        return true;
    }
};


}(jQuery));

基本上,您将整个匿名函数包装在您的 javascript 周围,将 jQuery 作为 $ 传递给匿名函数,它将在这个匿名函数中“覆盖”原型。

这有帮助吗?

于 2012-11-27T02:45:10.933 回答