Chrome 扩展:Youtube 中的内容脚本 (jQuery) 无法正常工作

发布于 2024-08-27 06:20:03 字数 668 浏览 6 评论 0原文

我有这个:

$(document).ready(function() {
    $("button.yt-uix-expander-arrow").attr("id", "yt-uix-expander-arrow");
    $("#yt-uix-expander-arrow").mouseover(function() {
        alert("Hello");
    });
});

注入 Youtube。使用右键单击>检查元素,

这是非常不寻常的,因为 #yt-uix-expander-arrow id 已经添加到按钮元素中。我在 Facebook 上测试了它,向类添加了一个 id,并在该 ID 上执行了 mouseover() 事件,它起作用了。

有什么想法吗?

I have this:

$(document).ready(function() {
    $("button.yt-uix-expander-arrow").attr("id", "yt-uix-expander-arrow");
    $("#yt-uix-expander-arrow").mouseover(function() {
        alert("Hello");
    });
});

injected into Youtube. Using right click > inspect element, the <button class="yt-uix-expander-arrow"> has a id="yt-uix-expander-arrow" attribute successfully added to it. However the mouseover event does not trigger. However if I were to change $("#yt-uix-expander-arrow").mouseover() to $(".yt-uix-expander-arrow").mouseover() it works.

That's very unusual, because the #yt-uix-expander-arrow id has already been added to the button element. I tested it out on Facebook, adding a id to a class and doing a mouseover() event on the ID and it works.

Any idea?

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

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

发布评论

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

评论(1

眼眸里的快感 2024-09-03 06:20:03

您能提供更多信息吗?你究竟想做什么(我的意思是不仅仅是显而易见的事情)?您能否以更容易测试的形式提供代码(即提供内容脚本或至少给出您尝试使用此代码的页面的 URL 以及内容脚本的摘要)?

您是否尝试将它们链接在一起?

$("button.yt-uix-expander-arrow").attr("id", "yt-uix-expander-arrow").mouseover(
    function() {
        alert("Hello");
    }
 );

我设置了我能做的最好的测试,你的代码工作得很好(我想,我再次不确定你到底想做什么)。如果没有更多背景,很难说哪里出了问题。

Can you provide more info? What exactly are you trying to do (I mean more than the obvious)? Can you provide code in a form that is more easily testable (i.e. provide the content script or at least give a url of a page you are trying to use this code on and a summary of the content script)?

Did you try just chaining them together?

$("button.yt-uix-expander-arrow").attr("id", "yt-uix-expander-arrow").mouseover(
    function() {
        alert("Hello");
    }
 );

I set up the best test I could and your code worked fine (I think, again I'm not sure EXACTLY what you're trying to do). It's hard to say what's wrong without more context.

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