导致jQuery"$(...)为null"的原因是什么?在某些页面上? [英] What causes jQuery "$(...) is null" on some pages?

查看:524
本文介绍了导致jQuery"$(...)为null"的原因是什么?在某些页面上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些JavaScript可以控制隐藏的div.现在它可以在大多数页面上使用,但是在其他带有其他JavaScript的页面上却无法使用...我的js写得不好吗?

I have some javascript which controls a hidden div. Now it works on most pages, but on other pages with other javascripts it is not working... Is my js written 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");
});
});

Firefox在错误控制台中报告了以下内容:

Firefox is reporting the following in the error console:

时间戳:4/18/2012 9:08:21 PM 错误:$(#user-dropdown-toggle")为空

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

推荐答案

jQuery中的$函数 永远不会返回null.是另一个正在使用的 non-jQuery $. (是否正在使用任何ASP.NET东西,prototype.js或其他库?稍后要声明的自定义$函数?)

The $ function in jQuery will never return null. So it is likely it is a different non-jQuery $ in use. (Is there any ASP.NET stuff or prototype.js or other library being used? A custom $ function declared later on?)

我知道$本身是 not null,因为例外情况会有所不同,例如:"$不是函数".在这种情况下,浏览器在说 表达式($(...))的结果是null,这使我得出了上述结论.

I know that $ is itself not null because the exception would be different, such as: "$ is not a function". In this case the browser is saying the result of the expression ($(...)) is null, which leads me to the above conclusion.

尝试:

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

或者更好的是,因为它仅依赖于window.jQuery而不是window.$:

Or, better yet, as it only relies on window.jQuery and not window.$:

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

而且,如果这行得通,那就是在就绪"事件之前(但在$(...).ready之后)正在破坏$.当然,此时(c0>已经 已经不是jQuery的可能性很大,但可能性很小).

And, if that works, then it is that $ is being clobbered before the "ready" event (but after $(...).ready). Of course, it is possible (but not very likely) that the $ is already not jQuery at this point...

要查看$是否为jQuery(使用Web开发者控制台或Firebug):

To see if $ is jQuery (use the Web Developer Console or Firebug):

  • $ === jQuery通常应为true,但可能不取决于noConflict
  • $.fn.jquery$().jquery都应导致jQuery版本为字符串
  • $(仅在控制台上指定功能将显示源")
  • $ === jQuery should generally be true, but it might not be depending on noConflict and such
  • $.fn.jquery and $().jquery should both result in the jQuery version as a string
  • $ (just specifying the function on the console will "display the source")

顺便说一句,我会缩进函数主体(例如,在就绪"回调中)以更清楚地了解它的开始/结束位置.

As an aside, I would indent the function body (e.g. in the "ready" callback) to see where it start/ends more clearly.

快乐的编码.

这篇关于导致jQuery"$(...)为null"的原因是什么?在某些页面上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆