What makes jQuery "$ (...) null" on some pages?

I have javascript that controls a hidden div. Now it works on most pages, but on other pages with other javascripts it does not work ... Did I write js poorly?

$(document).ready(function() {
$("#user-dropdown-toggle").live ('click', function() {
    $("#left-user-bar").addClass("open");
    $("#user-dropdown-toggle").addClass("league-judgement");
    $("body").addClass("league-judgement");
});
$(".league-judgement").live('click', function() {
  $("#left-user-bar").removeClass("open");
  $("#user-dropdown-toggle").removeClass("league-judgement");
  $("body").removeClass("league-judgement");
});
});

On the error console, Firefox reports the following:

Note: 04/18/2012 9:08:21 PM Error: $ ("# user-dropdown-toggle") is null

+5
source share
3 answers

A function $in jQuery will never return null. . Therefore, this is probably another non-jQuery $. (Is there any ASP.NET stuff or prototype.js or some other library in use? A custom function $declared later?)

, $ null, , : "$ ". , ($(...)) null, .

Try:

$(document).ready(function($) { // <-- note $ there
   ...
});

, , window.jQuery, window.$:

jQuery(function($) { // <-- note $ there
   ...
});

, , , $ "" ( $(...).ready). , ( ), $ jQuery ...

, $ jQuery ( - Firebug):

  • $ === jQuery , noConflict
  • $.fn.jquery $().jquery jQuery
  • $ ( " " )

, (, "" ), , / .

.

+9

, , , jQuery . , , $(document).ready, .

: jQuery $( "# field" ) null?

+1

jquery script/include.

" JS" .. , JQuery, , , - .....

0
source

All Articles