css hover 产生的一个Bug,怎么解决?

代码很简单,鼠标hover parent时,显示parent里面隐藏的child。问题在于,当随意多次点击child的时候,鼠标便会失去hover,child便消失了。请问如何解决?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .parent{
            width: 500px;
            height: 300px;
            background: #ccc;
            position: relative;
        }
        .parent:hover .child{
            display: block;
        }
        .child{
            width: 50px;
            height: 30px;
            background: red;
            position: absolute;
            top: 50px;
            left: 30px;
            cursor: pointer;
            display: none;
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="child"></div>
    </div>
</body>
</html>

鼠标一直在红色区域点击,鼠标失去hover(Google Chrome)图片描述

阅读 4.4k
4 个回答

<div class="child"></div>这里标签没关闭,但是你说的这个问题我在多个浏览器也没复现。

clipboard.png
题主,我在firefox,chrome和ie11下都测试了下,没出现点击红色区域后此区域消失的情况。你是怎么复现的?

具体原因不知,不过可以添加个样式解决

.parent:hover .child,
.child:hover{
    display: block;
}

未发现问题哦,楼主用的是什么浏览器呢

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题