Opera 用户 JS 中的 jQuery 不起作用

发布于 2024-09-01 15:50:03 字数 490 浏览 1 评论 0原文

)。我正在玩一些 Opera User JS。我在 User JS 文件夹中包含了“1jquery.min.js”(1 在前面,因为 Opera 按字母顺序加载它们)。不幸的是,它似乎不起作用。

window.onload = OnWindowLoad;

$(document).ready(function()
{
  alert ($('#area_19'));
});

function OnWindowLoad ()
{
  alert ($('#area_19'));
  alert(document.getElementById("area_19"));
}

这段代码的有趣之处在于,前两个警报返回 NULL,但最后一个警报确实找到了该对象!所以该元素肯定存在于页面中,但我的 jQuery 似乎无法获取它。更奇怪的是,jQuery“ready”功能起作用了,这表明我确实具有 jQuery 能力。

我对这一切感到很困惑::- /。希望有人能给我一个线索::-)。

). I'm playing with some Opera User JS. I included "1jquery.min.js" in my User JS folder (1 in front because Opera loads them alphabetically). Unfortunately, it doesn't appear to be working.

window.onload = OnWindowLoad;

$(document).ready(function()
{
  alert ($('#area_19'));
});

function OnWindowLoad ()
{
  alert ($('#area_19'));
  alert(document.getElementById("area_19"));
}

What's interesting about this code is that the first two alerts come back in NULL, but the last one does find the object! So the element definitely exists in the page, but my jQuery seems unable to get it. What's even stranger is that the jQuery "ready" function works, indicating that I do have jQuery capability.

I'm quite puzzled about all this ::- /. Hopefully somebody can give me a clue ::- ).

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

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

发布评论

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

评论(1

嘿嘿嘿 2024-09-08 15:50:03

我怀疑您正在使用另一个 JS 框架(可能是 Prototype.js)的页面上运行该脚本。

如果 Prototype 包含在目标页面中,它将用它自己的通过 ID(而不是选择器)获取元素的 jQuery 副本覆盖 $ 。由于没有 ID 为 #area_19 的元素(# 不是 ID 中的有效字符),因此它将返回 null。对于不存在的元素,jQuery 永远不会返回 null,您只会得到一个空的包装对象。

$(document).ready() 代码仍会执行,因为在包含 Prototype 之前调用了 $ 并更改了 $ 的行为.)

尝试使用显式的 jQuery 函数而不是 $ 快捷方式。

当混合多个框架,甚至混合同一框架的两个副本/版本时,此类干扰很常见。从 jQuery 的角度来看,使用 noConflict 可以减少但不能消除交互。就我个人而言,对于像用户脚本这样可能必须存在于不受我控制的广泛上下文中的代码,我会避免使用像 jQuery 这样的广泛框架。

I suspect you are running the script on a page that uses another JS framework, probably Prototype.js.

If Prototype were included by the target page it would overwrite your jQuery copy of $ with its own that gets an element by ID, not selector. Since there is no element with ID #area_19 (# not being a valid character in an ID), it would return null. jQuery would never return null for a non-existant element, you'd only get an empty wrapper object.

(The $(document).ready() code would still execute because the $ was called before Prototype was included and changed the behaviour of $.)

Try using the explicit jQuery function rather than the $ shortcut.

These sorts of interferences are common when mixing multiple frameworks, or even mixing two copies/versions of the same framework. From jQuery's side its interactions can be reduced, but not eliminated, with noConflict. Personally for code like user scripts that might have to live in a wide range of contexts not controlled by myself, I would avoid using wide-ranging frameworks like jQuery.

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