首页 > 解决方案 > 获取当前元素(JavaScript 中的内联函数)?

问题描述

我试图通过在 div 本身中创建一个函数来获取元素本身当有人点击当前元素时我想要的元素应该通过淡入纯 JavaScript 来删除我在互联网上搜索过但没有得到答案有很多类似的Stack Overflow 上的问题,但没有一个给出综合答案(比如通过 Js in(内联函数)获取当前元素)。所以任何人都可以帮助我

<div style="width: 100px;height: 100px;background:red;" onclick="(function(ele){
        console.log('helo')
        console.log(ele.currentTarget)})();">
提前致谢

标签: javascripthtmlcss

解决方案


这应该这样做:

我添加了一个id可以div通过getElementById.

<div id='hello' style="width: 100px;height: 100px;background:red;" onclick="
    (
        function() {
            document.getElementById('hello').style.opacity = 0;
            document.getElementById('hello').style.transition = '0.5s';
        }
    )();">


推荐阅读