简体   繁体   中英

Missing the following properties from type 'EventTarget'

Hello I have this piece of code in TypeScript

 var btn  = document.getElementsByClassName("btn-up") as EventTarget;
 btn.addEventListener('click', (event) => {
 console.log('clickcc');
})

And It throws me this error * Type 'HTMLCollectionOf' is missing the following properties from type 'EventTarget': addEventListener, dispatchEvent, removeEventListener *

Please how to fix it?

This will returns the array of elements, not just a single element:

document.getElementsByClassName("btn-up") as EventTarget

To:

document.getElementsByClassName("btn-up")[0] as EventTarget

WORKING DEMO:

编辑#SO-TS-EventTarget

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM