coding-style - 如何在 addEventListner 中设置不透明度样式?
问题描述
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 处设置未定义的属性“不透明度”。
解决方案
documents.getElementsByClassName
返回一个数组。
您应该使用方括号表示法访问要设置样式的元素
mainDiv[0].style.opacity = '0';
homeMainDiv[0].style.opacity = '1';
推荐阅读
- ruby-on-rails - 在 Rails 中更改 PostgreSql 用户密码时出现问题?
- python - InvalidToken cryptography.fernet.InvalidToken
- javascript - POST 请求后的 React hooks + Firebase 状态值直到硬刷新才会显示
- typescript - 仅在重载签名中出现错误 2589
- reactjs - 我的 React Project App 组件的一部分未显示
- macos - 尝试通过 pkgbuild 构建 macOS 应用程序,但在预安装或安装后它是 root,如何更改为普通用户并获取 sudo
- networking - 如果使用 scp.put() 发送文件时连接丢失,文件会发生什么情况?
- javascript - 如何在 D3.js 地图上创建标签
- css - 如何将 css-module 类名映射提取到 json 资产?
- javascript - 使用多个 Math.pow 将 Excel 公式转换为 Javascript