[英]CSS hover does not affect on other element
当我将CSS :hover
用于它自己的元素时,它可以工作,但是当我尝试影响另一个元素时,它没有任何作用。
例如,当我将鼠标悬停在该按钮上时,应显示隐藏的链接,但不会显示。
.dropdown { position: relative; display: inline-block; border: 1px solid black; width: 200px; } .dropbutton { width: 100%; height: 60px; color: white; background: #017678; border: none; } .dropcontent a { color: black; text-align: center; line-height: 40px; text-decoration: none; height: 40px; background-color: #DDD; border-width: 0 0 1px 0; border-style: solid; border-color: #9fa0a8; display: none; } a:last-of-type { border: none; } .dropbutton:hover .dropcontent { display: block; }
<div class="dropdown"> <button class="dropbutton">SHOW CONTENT</button> <div class="dropcontent"> <a href="#">c1</a> <a href="#">c2</a> <a href="#">c3</a> </div> </div>
我要移动display: none;
到.dropcontent
本身-因为它现在与它的锚点(即链接)有关,因此,当前答案都不起作用-然后使用
.dropbutton:hover + .dropcontent
{
display: block;
}
但是您之后不得在dropbutton和dropcontent之间添加任何内容,否则它将不再起作用。
您是否正在使用Javascript执行此操作?
var button = document.getElementsByClassName('.dropbutton')[0],
menuContent = docuemnt.getElementsByClassName('.dropcontent')[0];
button.onmouseover = function(event) {
menuContent.style.display['block'];
}
button.onmouseout = function(event) {
menuContent.style.display['none'];
}
稍微更改一下CSS: .dropbutton
应该具有display: none
,并且您应该从.dropcontent a
删除display: none
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.