如何避免JQuery和Prototype之间的冲突 [英] How to avoid conflict between JQuery and Prototype

查看:125
本文介绍了如何避免JQuery和Prototype之间的冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果页面同时包含JQuery和Prototype,那么我就会遇到冲突。有一个选项可以禁用JQuery的$ sign,所以没有冲突,但我必须使用关键字JQuery而不是$。

If a page has both JQuery and Prototype then I got conflict. There is a option to disable $ sign for JQuery so there is no conflict but, I have to use keyword JQuery instead of $.

我想知道是否有任何选项解决这种冲突的原型。有没有办法在不损害其利益或短关键字的情况下使用这两个库?

I wonder if there is any option for Prototype to solve this conflict. Is there any way to use both libraries without compromising their benefit or short keyword?

据我所知,使用多个JS库并不是一个好主意页;但有时可能会有所帮助。

As far as I know, it is not a good idea to use multiple JS library for same page; but it may be helpful for sometimes.

推荐答案

使用 noConflict 方法,并将其分配给新的(短)变量。使用$变量jQuery的新变量。

Use the noConflict method for jQuery and assign it to a new (short) variable. Use the new variable wherever you would have used the $ for jQuery.

var $j = jQuery.noConflict();

$j(function() {
    $j('#selector').each( .... );
});

或者,如果你不需要混合Prototype / jQuery,你可以包装所有的jQuery代码在匿名函数中。

or, if you don't need to mix Prototype/jQuery you can wrap all of your jQuery code in an anonymous function.

(function($) {
    // $ sign here is a parameter, which is set to jQuery 

    $(function() {
        $('#selector').each( .... );
    });
})(jQuery);

这篇关于如何避免JQuery和Prototype之间的冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆