gpt4 book ai didi

google-chrome-extension - Chrome 扩展程序 : Using addEventListener()

转载 作者:行者123 更新时间:2023-12-04 22:07:40 26 4
gpt4 key购买 nike

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=,而是在我的脚本代码中调用 addEventListener()。我尝试了几种方法,但显然我猜错了。这段代码会是什么样子?特别是,我在哪个对象上调用 addEventListener()?

谢谢!

最佳答案

我通常将它用于 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..
});
});

关于google-chrome-extension - Chrome 扩展程序 : Using addEventListener(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14665332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com