繁体   English   中英

Chrome Extension Javascript未触发操作

[英]Chrome Extension Javascript not triggering action

我和我的伙伴正在将第一个chrome扩展作为一个类项目创建,但似乎无法克服这一难题。 点击右上角的扩展程序图标后,我们将出现一个带有按钮的下拉菜单,单击该菜单后,它将触发一个javascript函数。 从顶部开始,我们创建了一个带有测试功能的虚拟按钮,该按钮应该触发console.log,但是无论我们做什么,都不会在控制台(或inspect popup控制台)中显示任何内容。

JS + HTML:

 document.getElementById("DOMContentLoaded", function () { var btnStart = document.getElementById('startSc'); btnStart.addEventListener('click', function () { console.log("hi"); }); }); 
 <!doctype html> <html> <head> <title>First Extension</title> <script src="popup.js"></script> <link rel='stylesheet' href="TBStyle.css"> </head> <body> <h1>Go Chrome!</h1> <a href="#" id="startSc">Start</a> </body> </html> 

清单:

 { "name": "My First Extension", "version": "1.0", "manifest_version": 2, "description": "The first extension that I made.", "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" }, } 

非常感谢您的任何帮助,在此先感谢您!

getElementById不能那样工作。

我相信您要寻找的是:

 document.addEventListener("DOMContentLoaded", function () { var btnStart = document.getElementById('startSc'); btnStart.addEventListener('click', function () { console.log("hi"); }); }); 
 <!doctype html> <html> <head> <title>First Extension</title> <script src="popup.js"></script> <link rel='stylesheet' href="TBStyle.css"> </head> <body> <h1>Go Chrome!</h1> <a href="#" id="startSc">Start</a> </body> </html> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM