【问题标题】:how to style opacity in addEventListner?如何在 addEventListener 中设置不透明度样式?
【发布时间】:2021-11-01 05:11:09
【问题描述】:
const mainDiv = document.getElementsByClassName('home-bg-image');
const homeMainDiv = document.getElementsByClassName('home-bg');
const homeDiv = document.getElementById('home-1');




homeDiv.addEventListener('mouseenter', e => {
    mainDiv.style.opacity = '0';
    homeMainDiv.style.opacity = '1';
    console.log('Working !!!')
});

我收到此错误 未捕获的类型错误:无法在 HTMLDivElement 处设置未定义的属性“不透明度”。

【问题讨论】:

    标签: coding-style mouseevent addeventlistener opacity mouseenter


    【解决方案1】:

    documents.getElementsByClassName 返回一个数组。

    您应该使用括号表示法访问要设置样式的元素

    mainDiv[0].style.opacity = '0';
    homeMainDiv[0].style.opacity = '1';
    

    【讨论】:

      猜你喜欢
      • 2011-05-26
      • 1970-01-01
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 2016-03-01
      • 2021-02-19
      相关资源
      最近更新 更多