which library is overriding the $? [duplicate]

2019-07-25 21:35发布

问题:

This question is an exact duplicate of:

  • jquery, bootstrap 3.0, and requirejs. can't use bootstrap's functions 2 answers

I'm trying to use jquery/bootstrap/ and requirejs

Loading js files works perfectly and I checked that bootstrap sets $.fn.tab = someFunction;

but then, inside requirejs's callback function, calling $(foo).tab('show') results in an error saying that tab is not available for the object.

Uncaught TypeError: Object [object Object] has no method 'tab'

Everything loads fine, I confirmed they do by printing a log.
But I guess $ is redefined or something.
Where should I start to look?
grep '$\s*=' ?

my code is rather long and I posted under a different question: jquery, bootstrap 3.0, and requirejs. can't use bootstrap's functions

I'm using django if that makes any difference

回答1:

The problem is likely that bootstrap hasn't finished loading at the time you try to use it. Try changing your bootstrap shim in the requirejs config to this:

shim: {
  "bootstrap": {
    deps: ["jquery"],
    exports: "$.fn.tab"
  }
}

RequireJS will wait to execute any code depending on bootstrap until $.fn.tab is set.



回答2:

Ok. U should use jquery no conflict function to avoid other library that use the same $ symbol.

$.noConflict();
jQuery( document ).ready(function( $ ) {
     // Code that uses jQuery's $ can follow here.
});

Now with many framework use $ such as prototypejs i think it is a good convention to follow. Reference :

  • http://requirejs.org/docs/jquery.html
  • http://api.jquery.com/jQuery.noConflict/