用户脚本,识别何时将元素添加到页面(通过事件?)

发布于 2024-09-02 08:27:24 字数 98 浏览 4 评论 0原文

现在,我正在使用一个自动更新的页面,并使用 setInterval 来检查新元素。我想用事件来做到这一点,但我不知道如何判断元素是否已通过事件添加到页面。

这可能吗?

Right now, I am working with a page that is automatically updated and I'm using setInterval to check for new elements. I'd like to do this with events but I don't know how to tell if an element has been added to the page via an event.

Is this possible?

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

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

发布评论

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

评论(1

兔小萌 2024-09-09 08:27:24

有一个突变事件可以完成这项工作,但 IE 不支持它(尽管 IE9 应该支持):

  • DOMNodeInserted

    <块引用>

    一个节点已被添加为另一个节点的子节点,或者在 Attr 节点的情况下,已被添加到一个元素。该事件应在插入发生后调度。该事件的目标节点应为正在插入的节点。

没有 IE 等效项,因此最好的办法是使用功能检测来查看它是否可用,如果不可用则回退到计时器方法。

There's a mutation event that should do the job, but it's not supported in IE (although it should be in IE9):

  • DOMNodeInserted

    A node has been added as a child of another node or, in case of Attr nodes, has been added to an Element. This event shall be dispatched after the insertion has taken place. The target node of this event shall be the node being inserted.

There is no IE equivalent, so the best thing to do is use feature detection to see if it is available and fall back to your timer method if it isn't.

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