<i id='bJY8q'><tr id='bJY8q'><dt id='bJY8q'><q id='bJY8q'><span id='bJY8q'><b id='bJY8q'><form id='bJY8q'><ins id='bJY8q'></ins><ul id='bJY8q'></ul><sub id='bJY8q'></sub></form><legend id='bJY8q'></legend><bdo id='bJY8q'><pre id='bJY8q'><center id='bJY8q'></center></pre></bdo></b><th id='bJY8q'></th></span></q></dt></tr></i><div id='bJY8q'><tfoot id='bJY8q'></tfoot><dl id='bJY8q'><fieldset id='bJY8q'></fieldset></dl></div>
<tfoot id='bJY8q'></tfoot>

<small id='bJY8q'></small><noframes id='bJY8q'>

    <bdo id='bJY8q'></bdo><ul id='bJY8q'></ul>

      <legend id='bJY8q'><style id='bJY8q'><dir id='bJY8q'><q id='bJY8q'></q></dir></style></legend>
      1. 当元素添加到页面时如何通知我?

        时间:2023-09-05
        1. <legend id='FHHQI'><style id='FHHQI'><dir id='FHHQI'><q id='FHHQI'></q></dir></style></legend>
          1. <tfoot id='FHHQI'></tfoot>

              • <i id='FHHQI'><tr id='FHHQI'><dt id='FHHQI'><q id='FHHQI'><span id='FHHQI'><b id='FHHQI'><form id='FHHQI'><ins id='FHHQI'></ins><ul id='FHHQI'></ul><sub id='FHHQI'></sub></form><legend id='FHHQI'></legend><bdo id='FHHQI'><pre id='FHHQI'><center id='FHHQI'></center></pre></bdo></b><th id='FHHQI'></th></span></q></dt></tr></i><div id='FHHQI'><tfoot id='FHHQI'></tfoot><dl id='FHHQI'><fieldset id='FHHQI'></fieldset></dl></div>
                  <tbody id='FHHQI'></tbody>

                  <bdo id='FHHQI'></bdo><ul id='FHHQI'></ul>
                • <small id='FHHQI'></small><noframes id='FHHQI'>

                  本文介绍了当元素添加到页面时如何通知我?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我希望在将 DOM 元素添加到页面时运行我选择的函数.这是在浏览器扩展的上下文中,因此网页独立于我运行,我无法修改其源代码.我在这里有什么选择?

                  I want a function of my choosing to run when a DOM element is added to the page. This is in the context of a browser extension, so the webpage runs independently of me and I cannot modify its source. What are my options here?

                  我想,理论上,我可以只使用 setInterval() 来不断搜索元素的存在并在元素存在时执行我的操作,但我需要更好的方法.

                  I guess that, in theory, I could just use setInterval() to continually search for the element's presence and perform my action if the element is there, but I need a better approach.

                  推荐答案

                  警告!

                  这个答案现在已经过时了.DOM Level 4 引入了 MutationObserver,为弃用的突变事件.请参阅 this answer 另一个问题,以获得比此处提供的更好的解决方案.严重地.不要每 100 毫秒轮询一次 DOM;它会浪费 CPU 资源,你的用户会讨厌你.

                  This answer is now outdated. DOM Level 4 introduced MutationObserver, providing an effective replacement for the deprecated mutation events. See this answer to another question for a better solution than the one presented here. Seriously. Don't poll the DOM every 100 milliseconds; it will waste CPU power and your users will hate you.

                  自从 变异事件在 2012 年被弃用,并且您无法控制插入的元素,因为它们是由其他人的代码添加的,您唯一的选择是不断检查它们.

                  Since mutation events were deprecated in 2012, and you have no control over the inserted elements because they are added by someone else's code, your only option is to continuously check for them.

                  function checkDOMChange()
                  {
                      // check for any new element being inserted here,
                      // or a particular node being modified
                  
                      // call the function again after 100 milliseconds
                      setTimeout( checkDOMChange, 100 );
                  }
                  

                  一旦调用此函数,它将每 100 毫秒运行一次,即 1/10(十分之一)秒.除非您需要实时元素观察,否则应该足够了.

                  Once this function is called, it will run every 100 milliseconds, which is 1/10 (one tenth) of a second. Unless you need real-time element observation, it should be enough.

                  这篇关于当元素添加到页面时如何通知我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:jQuery复选框更改和单击事件 下一篇:jQuery 复选框选中状态更改事件

                  相关文章

                  最新文章

                    <tfoot id='Q9Zbc'></tfoot>
                    1. <legend id='Q9Zbc'><style id='Q9Zbc'><dir id='Q9Zbc'><q id='Q9Zbc'></q></dir></style></legend>
                        <bdo id='Q9Zbc'></bdo><ul id='Q9Zbc'></ul>
                      <i id='Q9Zbc'><tr id='Q9Zbc'><dt id='Q9Zbc'><q id='Q9Zbc'><span id='Q9Zbc'><b id='Q9Zbc'><form id='Q9Zbc'><ins id='Q9Zbc'></ins><ul id='Q9Zbc'></ul><sub id='Q9Zbc'></sub></form><legend id='Q9Zbc'></legend><bdo id='Q9Zbc'><pre id='Q9Zbc'><center id='Q9Zbc'></center></pre></bdo></b><th id='Q9Zbc'></th></span></q></dt></tr></i><div id='Q9Zbc'><tfoot id='Q9Zbc'></tfoot><dl id='Q9Zbc'><fieldset id='Q9Zbc'></fieldset></dl></div>

                      <small id='Q9Zbc'></small><noframes id='Q9Zbc'>