javascript - CSS:隐藏另一个div

标签 javascript html css siblings

我的代码中有一个父 div 和 2 个子 div。我希望第一个 child 悬停,第二个 child 隐藏。我只想使用 css 或 js 来做到这一点。

Here's my Fiddle

<div class="parrent">
    <div id="child1">
        Hide child2
    </div>

    <div id="child2">  
        I must hide
    </div>
</div>

谢谢!

最佳答案

使用这个:

#child1:hover ~ #child2 {
    visibility:hidden;
}

Demo

这使用了 General sibling combinator ~

~ selector: The general sibling combinator selector is very similar to the adjacent sibling combinator selector we just looked at. The difference is that that the element being selected doesn't need immediately succeed the first element, but can appear anywhere after it.


您还可以使用 Adjacent sibling combinator +,具体取决于您的其余代码。

+ selector: An adjacent sibling combinator selector allows you to select an element that is directly after another specific element.

#child1:hover + #child2 {
    visibility:hidden;
}

Demo

关于javascript - CSS:隐藏另一个div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21215975/

相关文章:

html - 按钮未在 block 级容器中居中

javascript - 如何使用 Javascript 从两个文本框获取值并在单击按钮时显示在第三个文本框中?

html - 如何使用图标制作 CSS 简单进度条(仅查看以显示)?

html - 将 Dojo 样式应用于按钮时显示的两个按钮

javascript - 导入 angularjs 缩小版

javascript - 无法使用 ant 和闭包编译器编译 javascript,因为 Jquery 的 $ 未声明

javascript - 我应该如何将函数传递给我的 React JSX 组件?

javascript - 关于javascript文件中相对值的问题

html - 根据屏幕分辨率调整iFrame的高度

javascript - 2个相邻的div,中间有很小的间距