![](/img/trans.png)
[英]Request keeps on getting Error code: 400 - can someone please have a look on what I'm doing wrong?
[英]I have been adding an event Listener to this button and getting an error. What I'm I doing wrong?
我一直在尝试向此按钮添加事件侦听器并收到 Uncaught TypeError: Cannot read property 'addEventListener' of null
let button = document.getElementById('submit'); function hello() { console.log("hello"); } button.addEventListener('click', hello);
<div class="button"> <button id="submit">Submit</button> </div>
也许是因为 js 在 html 之前加载。 尝试将您的 js 代码放入此 function 中:
window.onload = () => {
\\ your js code
}
代码看起来很好,实际上正在工作!
但是,当您忘记或错误输入元素的 id/class 时,通常会发生此问题。 所以当脚本尝试监听事件时,他无法找到元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.