javascript - 未捕获的类型错误 : Cannot set properties of undefined (setting 'display' )

标签 javascript css reactjs

我正在为我的音乐 react 应用程序制作一个进度条。我想让进度指示器在用户将鼠标悬停在进度栏容器上时显示。但我面临这个错误:未捕获的类型错误:无法设置未定义的属性(设置“显示”)

我的 JavaScript:

document.getElementsByClassName('rhap_progress-container')[0].onmouseover = function(){
  document.getElementsByClassName('rhap_progress-indicator').style.display = "block";
}
document.getElementsByClassName('rhap_progress-container')[0].onmouseout = function(){
  document.getElementsByClassName('rhap_progress-indicator').style.display = "none";
}

我的html:

<div class="rhap_progress-container" aria-label="Audio progress control" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="24.67" tabindex="0">
  <div class="rhap_progress-bar ">
    <div class="rhap_progress-indicator" style="left: 24.67%;"></div>
    <div class="rhap_progress-filled" style="width: 24.67%;"></div>
  </div>
</div>

我的CSS:

.rhap_progress-indicator {
  display: none;
  width: 18px !important;
  height: 18px !important;
  border: 3px solid #232530;
  top: -7 px !important;
  box-shadow: none !important;
  opacity: 1 !important;
  content: url('./img/outlined-progress-indicator.png');
}

最佳答案

document.getElementsByClassName 返回一个数组。

因此 document.getElementsByClassName('rhap_progress-indicator') 还需要选择数组的索引:

document.getElementsByClassName('rhap_progress-indicator')[0]

另一种方法是使用 document.querySelector('.rhap_progress-indicator'),它会返回元素(如果存在)。

关于javascript - 未捕获的类型错误 : Cannot set properties of undefined (setting 'display' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71807404/

相关文章:

javascript - (Foundation CSS) 下拉菜单不起作用

javascript - 在多个 div 中具有相同类的元素外部单击时如何隐藏特定的 div

jQuery 添加删除类到 id IF

javascript - ReactJS CSS 类不工作

javascript - promise 链接拒绝

javascript - 在三星智能电视应用程序中实现内容滚动条

javascript - 停止滚动时如何停止菜单

node.js - 安装React应用程序时出错,找不到我的文件夹的路径

javascript - 动态解构 Prop ?

javascript - 使用物理引擎在框架中移动相机