简体   繁体   English

我在使用事件时在控制台中遇到了一些错误

[英]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

Check my code检查我的代码

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.

相关问题 在SPFx项目中使用光滑轮播。 我正在加载媒体库,但出现一些奇怪的控制台错误 - Using slick carousel in SPFx project. I am loading the Library but getting some weird console errors 为什么我在这个 nodejs 天气应用程序的控制台中收到这些错误? - Why am I getting these errors in my console on this nodejs weather app? 我在控制台中收到此错误:无法读取未定义的属性“ some” - I am getting this error in the console : Cannot read property 'some' of undefined 我在使用 nodemailer 时遇到错误 - i am getting error while using nodemailer 我在使用复选框时遇到问题 - I am getting a problem while using checkboxes 在 cmd 中安装材料 ui 以进行反应时出现错误。我正在使用此命令 npm i @material-ui/icons 并出现以下错误:: - I am getting error while installing material ui in cmd for react .I am using this command npm i @material-ui/icons and getting following errors :: 在使用Codeigniter进行Soap调用期间出现控制台错误 - I am getting console error during soap call using codeigniter 我收到2美元的未定义错误 - I am getting 2 $ in not defined errors Discord.JS - 我收到“应用程序没有响应”但控制台中没有错误 - Discord.JS - I am getting 'Application did not respond' but no errors in console 为什么使用js服务器通过php发送事件会看到未指定的sse错误? - Why am I seeing unspecified sse errors using js server sent events with php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM