Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

javascript - How to be a jQuery no.conflict expert?

How to be a jQuery no.conflict expert?

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.

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

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
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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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

becomes

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

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.

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.)

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...