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

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

问题描述

我有一些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 之后)破坏了 $ .当然,这时可能(但不太可能) $ 已经 而不是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&amp;"$(...)为null&amp; quot;在某些页面上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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