【问题标题】:Selecting multiple adjacent siblings (and their children)选择多个相邻的兄弟姐妹(及其孩子)
【发布时间】:2014-07-06 19:43:05
【问题描述】:

当我将鼠标悬停在 div 上时,如何更改其 2 个兄弟姐妹的样式?

#magic:hover + (#sibling1 > span, #sibling2 > span){color: blue}

JSFiddle

【问题讨论】:

标签: html css css-selectors


【解决方案1】:

为什么不使用相邻选择器来实现呢?

#magic:hover + #sibling1 + #sibling2 span {
    color: red;
}

Demo (悬停第一个元素,查看第三个元素的颜色变化)

如果你想改变#magic后面的所有元素的样式,比使用

#magic:hover + #sibling1 span,
#magic:hover + #sibling1 + #sibling2 span {
    /* Styles goes here */
}

Demo 2

【讨论】:

    【解决方案2】:

    更新了这个fiddle

    #magic:hover ~ .box span {color: blue}
    

    编辑改用上面的,显然 + 只适用于直接兄弟http://www.sitepoint.com/web-foundations/adjacent-sibling-selector-css-selector/

    【讨论】:

    • 不幸的是,这只改变了第一个兄弟的风格
    • 这将选择以下兄弟姐妹,您不能单独使用 css 选择较早的兄弟姐妹。
    猜你喜欢
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多