How to give the $ sign back to jQuery?
I am already declaring jQuery at the top of my <head>
, below that I am referencing a ton of other plugins. When I do this in the body of my page:
<script type="text/javascript">
$(function() {
alert('hello');
});
</script>
I don't get the alert. However if I redeclare jQuery again like so:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
alert('hello');
});
</script>
It works. I开发者_如何学Cs there any way to reassign the $ back to jQuery (which is what I'm guessing is the problem here). So I can use $ instead of jQuery noConlict.
<script type="text/javascript">
jQuery(function($) {
alert('hello');
//use $ however u want
});
</script>
Fiddle (using $_
): http://jsfiddle.net/maniator/B3hU4/
You are placing your jQuery inline on the page rather than in a document ready block. If you don't have them in a document ready block, there's no guarantee that jQuery has fully loaded before the function is fired.
Encapsulate like this:
<script type="text/javascript">
(function($) {
alert('hello');
})(jQuery);
</script>
You can also just do this if you want $ returned to be jQuery globally:
window.$ = jQuery;
精彩评论