最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery conflicting with other libraries - Stack Overflow

programmeradmin0浏览0评论

I am facing trouble in jQuery implementation, my jQuery code have been conflicted in this. I am using 'latest.pack.js' with 'source.js' and 'prototype.js'.

Above mentioned all jQuery not working reason may be conflicting and jQuery no conflicting code is also not working there . So Please help me soon as possible

Link are : .js

I am facing trouble in jQuery implementation, my jQuery code have been conflicted in this. I am using 'latest.pack.js' with 'source.js' and 'prototype.js'.

Above mentioned all jQuery not working reason may be conflicting and jQuery no conflicting code is also not working there . So Please help me soon as possible

Link are : http://prototypejs/assets/2009/8/31/prototype.js

Share Improve this question edited Mar 13, 2012 at 11:34 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked Mar 13, 2012 at 11:29 user1266321user1266321 12 bronze badges 0
Add a ment  | 

6 Answers 6

Reset to default 5

If at all possible, do yourself the favour and use one JavaScript library only.

  • Loading two full-blown libraries can be resource-heavy, especially if they both walk through the entire DOM to initialize some widgets or something.

  • Even with noConflict it is possible for problems to occur. Libraries use different internal methods to manipulate events and DOM nodes. There is the possibility of subtle bugs that noConflict is unable to prevent.

  • You will have two syntaxes and philosophies to work with.

  • You will have two libraries to update and check dependencies for.

Pick one, and select your plugins and widgets based on that one library.

Lots of libraries use the $ symbol .. you need to use the noConflict() jQuery method like so :

jQuery.noConflict();
// then your jQuery code ... for example
jQuery(document).ready(function($) {

or you could use a different variable for jQuery :

var j = jQuery.noConflict();
j(document).ready(function($) {

The most simple solution is to wrap your JQuery code with an anonymous function:

(function($) {
   // your jQuery code
   $('#foo');
})(jQuery);

Though I would remend to only use one library if possible. Multiple libraries clutter the code, increase the page load time and can produce unexpected results.

You should use the noConflict method.
Another way to get rid of the conflict would be to separate your jquery code from the other libs :

(function($){
    //jquery code here  
    // $ represents jQuery in this block 
    alert($ === jQuery);     
})(jQuery); 
// outside the block, $ is set to the other libs that overwrite this variable
alert($ === jQuery);

“Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery’s case, $ is just an alias for jQuery, so all functionality is available without using $. If we need to use another JavaScript library alongside jQuery, we can return control of $ back to the other library with a call to $.noConflict():"

<script type="text/javascript" src="other_lib.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$.noConflict();
jQuery(document).ready(function($) {
// Code that uses jQuery's $ can follow here.
});
 // Code that uses other library's $ can follow here.
</script>

jQuery documentation site has a link in main menu "Using jQuery With Other Libraries" with full instructions on implementing noConflict()

http://docs.jquery./Using_jQuery_with_Other_Libraries

发布评论

评论列表(0)

  1. 暂无评论