根据班级添加弹出窗口最有可能

问题描述

我正在尝试使用Tampermonkey在Canvas LMS中的页面上添加一个弹出窗口。这是一个论坛,每个帖子之后都有一个“答复”选项,这是我要添加弹出窗口的选项。但是,当我单击“答复”链接时,没有弹出窗口出现。像往常一样,它会打开“回复”框,但我的弹出窗口却看不见。

代码大致如下:

<div class="entry-controls hide-if-collapsed hide-if-replying">
  <div class="notification" data-bind="notification"></div>
      <a role="button" class="discussion-reply-action entry-control" data-event="addReply" href="#">
      <i class="icon-replied"></i>
      <span aria-hidden="true">Reply</span>
      <span class="screenreader-only">Reply to Comment</span>
    </a>
</div>

我要添加的JS代码是:

document.querySelectorAll('.discussion-reply-action').forEach(item => {
  item.addEventListener('click',event => {
    alert("Popup text here");
  })
})

除了.discussion-reply-action外,我还尝试使用.entry-controls.notification.entry-control,甚至是span[aria-hidden="true"]之类的东西。似乎什么都没有。

我知道Tampermonkey脚本本身可以正确应用,因为它具有像往常一样显示的其他功能。

有人知道为什么这对我不起作用吗?我是一个完整的JS新手,这是值得的。

解决方法

这个问题在答复中得到了回答,但是只是想正式指出它归结为延迟了我的代码注入。我试图附加到在文档之后加载的元素。一旦我支持他们,它就可以正常工作。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...