Why it is needed $ = jQuery
Question is nearly theoretical. But last time I've encountred some subtle bug. When the 开发者_JAVA技巧page contains child frame and both parent and child frame are used the same js file, if I not set $ = jQuery on the start, functionality of this js file crashed. What the trick could be here?
Thanks!
If there are other JavaScript libraries included anywhere in the page, they might use $
. Mootools and Prototype both use $
.
If you include jQuery and then you include Prototype then the $
variable will be over-written, so you'd have to set $ = jQuery
before any jQuery code would work:
<link rel="stylesheet" href="jQuery.js" />
<link rel="stylesheet" href="prototype.js" />
<script>$ = jQuery</script>
Equally, as @Guffa says, if you called noConflict anywhere it would unset the $
variable.
Ideally, I don't think you should use the $
variable, as it can cause problems. Just do something like:
jQuery.noConflict();
var jQ = jQuery;
And then use the jQ
variable as you would have the $
. Then you won't get conflicts.
That should not be needed unless you removed the reference from the $
variable.
Check if you are using the noConflict method somewhere to decouple jQuery from the $
variable, or have something else (like a function) named $
.
精彩评论