【问题标题】:Chrome Extension: Using addEventListener()Chrome 扩展:使用 addEventListener()
【发布时间】:2013-02-02 18:59:01
【问题描述】:

tutorial for migrating a Google Chrome Extension to Manifest Version 2 中,我被指示从 HTML 代码中删除内联事件处理程序(如 onclick 等),将它们移动到外部 JS 文件中并改用 addEventListener()。

好的,我目前有一个类似这样的 background.html 页面……

<html>
<script type="text/javascript">
    // Lots of script code here, snipped
    …
</script>

<body  onload="checkInMyNPAPIPlugin('pluginId');">
    <object type="application/x-mynpapiplugin" id="pluginId">
</body>
</html>

根据另一个指令,我已将 大量脚本代码 移动到一个单独的 .js 文件中,并且在此指令之后,我需要从 body 标记中删除 onload=,而不是 cal addEventListener() 在我的脚本代码中。我尝试了几种方法,但显然猜错了。该代码会是什么样子?特别是,我应该在什么对象上调用 addEventListener()?

谢谢!

【问题讨论】:

    标签: google-chrome-extension addeventlistener


    【解决方案1】:

    我通常将它用于 body onload 事件...

    document.addEventListener('DOMContentLoaded', function () {
        //   My code here.. ( Your code here  )
    });
    

    对于某些东西它正在工作..但实际上,我认为我们应该使用..

    window.addEventListener('load', function () {
        document.getElementById("#Our_DOM_Element").addEventListener('change - or - click..', function(){
    //      code..
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2018-10-24
      • 1970-01-01
      • 2012-03-25
      • 2020-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 2013-07-20
      相关资源
      最近更新 更多