为什么我无法在 JavaScript 中使用“this”关键字访问当前元素?它可以在 jquery 中工作,但我想学习普通的 javscript 等效项,但我似乎找不到答案。
document.getElementById('icon').onclick = () => {
this.style.display = 'none'
console.log(this) // prints the window instead of element?
}
最佳答案
document.getElementById('icon').onclick = ( ) => {
event.currentTarget.style.display='none';
}
<div id='icon'>xxx</div>
关于javascript - 在 javascript onevent 函数中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60765213/