本文介绍了ElementById查找元素,但不会添加事件侦听器(Chrome扩展程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的扩展程序的弹出窗口中有一个带有代码的表单:

I have a form in my extension's popup with the code:

<button type="submit" id="update" class="btn btn-primary">Submit</button>

然后在附带的脚本中,输入以下代码:

And then in the attached script, the following code:

document.addEventListener("DOMContentLoaded", function(event){
  var button = document.getElementById("update");
  button.addEventListener("click", test);
  button.style.color="red";
});



function test(){
  console.log("test");
}

该代码的"button.style.color =" red"部分有效,因此我知道该按钮已成功找到.但是,加载后以及单击按钮时,我仍然收到此错误:

The "button.style.color="red"" portion of the code works, so I know that the button was found successfully. However, I still receive this error upon load and when the button is clicked:

Uncaught TypeError: Cannot read property 'addEventListener' of null

我该如何解决这个问题:(

How do I fix this :(

推荐答案

这可能是重复的,但是,如果您仍然无法详细解释,请尝试使用这些解决方案.

This could be a possible duplicate Cannot read property 'addEventListener' of null but, try these solutions out, if your still having trouble explain more in detail.

这篇关于ElementById查找元素,但不会添加事件侦听器(Chrome扩展程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 04:31