[英]I am getting some errors in console while using events
I was trying click events while I open my browser console I am getting this error: events.js:2 Uncaught TypeError: button.addEventListener is not a function at events.js:2 (anonymous) @ events.js:2我在打开浏览器控制台时尝试单击事件我收到此错误: events.js:2 Uncaught TypeError: button.addEventListener is not a function at events.js:2 (anonymous) @ events.js:2
Function getElementsByTagName
returns an array of all buttons, so if you have only one button you can do document.getElementsByTagName('button')[0]
. Function getElementsByTagName
返回所有按钮的数组,所以如果你只有一个按钮,你可以做document.getElementsByTagName('button')[0]
。
document.getElementsByTagName
returns an HTML collection. document.getElementsByTagName
返回一个 HTML 集合。 You can either select a specific one using it's index: document.getElementsByTagName('button')[0]
or loop through and add an event listener to each one:您可以 select 使用它的索引来指定一个特定的: document.getElementsByTagName('button')[0]
或循环并为每个事件添加一个事件侦听器:
for (let i of buttons) {
i.addEventListener('click', () => {
console.log('CLICK!!!')
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.