什么可能导致 JQuery 选择器损坏?

发布于 2024-07-12 12:12:53 字数 204 浏览 4 评论 0原文

我试图使用 JQuery 的选择器获取一个元素,但即使该元素实际存在,它也找不到它。 此外,我实际上可以使用 getElementById 找到该元素。

例如:

$('#outputDiv') 给我一个空值。 但是 document.getElementById("outputDiv") 返回我试图访问的 Div。

任何想法?

I am trying to get an element using JQuery's selector, but it does not find it even though the element actually exists. Moreover, I can actually find the element using getElementById.

For example:

$('#outputDiv') gives me a null value. But document.getElementById("outputDiv") returns me the Div I was trying to access.

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光病人 2024-07-19 12:12:53

检查 jQuery 是否实际加载。 您是否使用 Firebug 或其他调试器进行过测试? 是否报告 JavaScript 错误? 您是否使用另一个使用 $ 作为别名的库?

Check that jQuery is actually loaded. Have you tested with Firebug or another debugger? Are javascript errors reported? Do you use another library that uses $ as an alias?

私藏温柔 2024-07-19 12:12:53

您是否使用任何其他可能使 $ 过载的库? 我知道例如原型就是这样做的。

您可能想尝试发出 jQuery('#outputDiv') 来查看它是否实际上是您正在调用的 jQuery。

Are you using any other libraries that might overload $ ? I know that e.g. that Prototype does this.

You might want to try issuing a jQuery('#outputDiv') instead to see if it is actually jQuery that you are invoking.

朱染 2024-07-19 12:12:53

如果您使用 IE7,事情发生的顺序非常重要,如果预先声明了 jQuery 脚本(即在 head 元素中),选择器往往不适用于动态生成的内容。

我们解决这个问题的一个方法是将代码放在文档的末尾。 尽管这听起来很奇怪,但 MS 将其记录为已存在多年的错误。

If you're using IE7 the order of when things happen are very important, and selectors don't tend to work for dynamically generated content if the jQuery script is declared beforehand, i.e. in the head element.

A way we got around this was to place the code at the end of the document. As weird as this sounds, it is documented by MS as a bug that's existed for many years.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文