【发布时间】:2021-04-19 22:21:19
【问题描述】:
当我将鼠标悬停在 div 上并隐藏其中的文本时,我试图在 div 中显示按钮,然后隐藏按钮但显示 div 的文本。我不断收到未定义的错误。我做错了什么?
错误:
coreScript.js:12 Uncaught TypeError: Cannot set property 'display' of 未定义
在 showButton (coreScript.js:12)
在 HTMLDivElement.onmouseover (Core.html:33)
coreScript.js:6 Uncaught TypeError: Cannot set property 'display' of 未定义
在 hideButton (coreScript.js:6)
在 HTMLDivElement.onmouseout (Core.html:33)
function hideButton() {
document.getElementsByClassName('xOne').style.display = 'none';
document.getElementsByClassName('xTen').style.display = 'none';
document.getElementsByClassName('all').style.display = 'none';
document.getElementsByClassName('name').style.display = 'block';
}
function showButton() {
document.getElementsByClassName('xOne').style.display = 'block';
document.getElementsByClassName('xTen').style.display = 'block';
document.getElementsByClassName('all').style.display = 'block';
document.getElementsByClassName('name').style.display = 'none';
}
.main-div {
border: 1px solid black;
min-width: 198px;
max-width: 198px;
min-height: 33px;
max-height: 33px;
font-size: 16px;
vertical-align: middle;
background-color: #fff;
margin: 5px;
line-height: 34px;
overflow: hidden;
text-align: center;
cursor: pointer;
animation: boxShadowNone 0.1s;
box-shadow: none;
}
<div class="main-div" onmouseover="showButton()" onmouseout=" hideButton()" onclick="sell()" id="sellMeat">
<div class="name">SELL MEAT</div>
<div class="xOne" style="display: none;">x1</div>
<div class="xTen" style="display: none;">x10</div>
<div class="all" style="display: none;">All</div>
</div>
<br>
<div class="main-div" onmouseover="showButton()" onmouseout="changeText(this, 'SELL LEATHER', '16px'); hideButton()" onclick="sell()" id="sellLeather">
<div class="name">SELL Leather</div>
<div class="xOne" style="display: none;">x1</div>
<div class="xTen" style="display: none;">x10</div>
<div class="all" style="display: none;">All</div>
</div>
【问题讨论】:
标签: javascript html