'addEventListener'在Chrome扩展程序上不起作用 [英] 'addEventListener' not working on Chrome Extension
本文介绍了'addEventListener'在Chrome扩展程序上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在窗口中添加了一个addEventListener,但它返回以下错误:
I added an addEventListener to my window, but it is returning the following errors:
未捕获的TypeError:无法在'EventTarget'上执行'addEventListener':必需2个参数,但仅存在1个.在
Uncaught TypeError: Failed to execute 'addEventListener' on 'EventTarget': 2 arguments required, but only 1 present. at
window.addEventListener('DOMContentLoaded', setUpStuff, false);
另一个错误:
未捕获的TypeError:无法读取null的属性"addEventListener"(在:
Uncaught TypeError: Cannot read property 'addEventListener' of null (at:
optionsButton.addEventListener('click', function() {
这是代码:
window.addEventListener('DOMContentLoaded', setUpStuff, false);
function setUpStuff(){
let optionsButton = document.getElementById('#go-to-options');
optionsButton.addEventListener('click', function() {
if (chrome.runtime.openOptionsPage) {
chrome.runtime.openOptionsPage();
} else {
window.open(chrome.runtime.getURL('options.html'));
}
});
}
推荐答案
您应该在函数执行时添加一个事件.而且getElementById
需要一个ID,而不是选择器,因此您需要删除#
:
You are supposed to add an event on the execution of which the function is going to be run. And getElementById
takes an ID, not a selector, so you need to remove the #
:
window.addEventListener('DOMContentLoaded', setUpStuff, false);
function setUpStuff(){
let optionsButton = document.getElementById('go-to-options');
optionsButton.addEventListener('click', function() {
if (chrome.runtime.openOptionsPage) {
chrome.runtime.openOptionsPage();
} else {
window.open(chrome.runtime.getURL('options.html'));
}
});
}
这篇关于'addEventListener'在Chrome扩展程序上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文