如何成为一个jQuery no.conflict专家? [英] How to be a jQuery no.conflict expert?

查看:104
本文介绍了如何成为一个jQuery no.conflict专家?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何成为jQuery no.conflict专家?

How to be a jQuery no.conflict expert?

我主要面对与Prototypes JS的jQuery冲突错误。而且我不是jquery专家。

I mostly face jQuery conflict errors with Prototypes JS. and I'm nota jquery expert.

我还能解决所有冲突问题。如何获得专业化解决冲突问题。

Can I still solve all conflict problems. how to get expertize to solve conflict issues.

如何知道整个代码是jquery。如果简单的javascript和jquery代码混合在一个js文件中怎么办?

How to know whole code is jquery. what if simple javascript and jquery code mixed in one js file?

如果内部外部js代码直接从

What if inside external js code directly begin from

       var opbox="";

这是我无法更改的来源订单

this is source order Which i can't change

1

<script type="text/javascript" src="jquery.min.js"></script> 

2

我想删除冲突错误来自example.js

2
I want to remove conflict error from example.js

<script type="text/javascript" src="example.js"></script>

3

<script type="text/javascript" src="Prototype.js"></script>

4

<script type="text/javascript" src="scriptaculous.js ></script>


推荐答案

问题是,如果你想在页面上使用Prototype, $ 必须是Prototype的 $ ,而不是jQuery的。因为 $ 只是 jQuery的别名无论如何,jQuery提供 noConflict 作为一种方法告诉jQuery将 $ 的控制权返回到jQuery加载之前的任何内容。

The issue is that if you want to use Prototype on the page, $ must be Prototype's $, not jQuery's. Since $ is just an alias for jQuery anyway, jQuery provides the noConflict function as a means of telling jQuery to return control of $ back to whatever had it before jQuery was loaded.

这意味着无论何时你使用 $ ,你都会使用 jQuery ,例如:

That means whenever you might use $, you'd use jQuery instead, e.g.:

$('.foo').hide(); // Hide all elements with class "foo"

变为

jQuery('.foo').hide(); // Hide all elements with class "foo"

如果您正在使用外部脚本,并且他们使用 $ ,你必须修改它们。一种方法是进行搜索和替换,只需将所有(适当的) $ 更改为 jQuery

If you're using external scripts, and if they use $, you have to modify them. One way is to do a search-and-replace and just change all the (appropriate) $'s to jQuery.

另一种方法是把它放在你的脚本标签之后,包括jQuery:

Another way is to put this just after your script tag including jQuery:

<script type='text/javascript'>jQuery.noConflict();</script>

...然后将其添加到外部脚本的顶部:

...and then add this at the top of the external script:

(function($) {

...这就在它的底部:

...and this at the bottom of it:

})(jQuery);

(是的,这意味着你必须修改外部脚本。)

(Yes, this means you have to modify the external scripts.)

这样做是将整个外部脚本放在一个函数中,其中 $ 符号被解析为 jQuery 而不是Prototype的 $ ,因为 $ 是该函数的参数,所以它需要优先。但是,该技术将失败一些脚本。如果他们希望他们的函数声明将在全局范围内发生(例如,成为 window 对象的属性),那么将整个脚本放在一个函数中就会破坏它。在这些情况下,您必须通过将这些功能分配到窗口来手动导出这些功能,或者使用上面的全局搜索和替换选项。

What that does is put the entire external script inside a function, within which the $ symbol is resolved to jQuery rather than to Prototype's $, because $ is an argument to that function and so it takes precedence. However, that technique will fail with some scripts. If they expect that their function declarations will happen at global scope (e.g., become properties of the window object), then putting the entire script in a function will break that. In those cases, you have to either manually export those functions by assigning them to window or use the global search-and-replace option above.

在jQuery noConflict 页面上有这样的例子,尽管他们经常将上面的内容与 ready 函数,这可能有点令人困惑。

There are examples of this on the jQuery noConflict page, although they frequently combine the above with the ready function, which might be a bit confusing.

注意bobince关于框架之间冲突的观点,不仅限于 $ 符号。如果您可能只使用一个框架,那就这样做。

Note bobince's point about conflicts between frameworks not just being limited to the $ symbol. If you can possibly stick to using just one framework, do that.

这篇关于如何成为一个jQuery no.conflict专家?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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