javascript - 在 javascript onevent 函数中使用它

标签 javascript html

为什么我无法在 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/

相关文章:

javascript - 如何在拖动元素时逐渐降低其不透明度?

javascript - 如何使无法通过浏览器控制台更改变量

javascript - Worklight - Paho MQTT JavaScript : Always timeout on Android device and works in simulator

javascript - 中断 CSS 动画导致奇怪的转换

javascript - 如何获取xml中所有的一级嵌套 Node ?

javascript - React-Native 在 MX Video Player 中使用 Intent 打开 Url

javascript - Jquery响应加载

jquery - 使用 jquery 删除没有 <dd> 的 <dt>

css - 将 div 元素置于另一个 div 元素的中心

javascript - CSS/cut an img's width when reducing the screen/window width 减少屏幕/窗口宽度