加号选择器(+)用于选择下一个相邻同级。

前一个兄弟姐妹是否有同等的?


当前回答

不,没有“上一个同级”选择器。

在一个相关的注释中,~表示一般继承兄弟(意味着元素在这个之后,但不一定紧接着),并且是一个CSS3选择器。+用于下一个兄弟姐妹,为CSS2.1。

请参阅选择器级别3中的相邻同级组合符和级联样式表级别2修订版1(CSS 2.1)规范中的5.7相邻同级选择器。

其他回答

选择器级别4建议:has()(以前是主题指示符!),有一天,它将允许您选择前一个兄弟姐妹:

previous:has(+ next) {}

或(对于一般上一个兄弟姐妹而不是相邻兄弟姐妹):

previous:has(~ next) {}

在撰写本文时:has{}被大多数但不是所有主要浏览器支持。支持正在改善。


多年来,这个答案吸引了数十条“仍然不支持”的评论(现已删除)。请不要再添加了。答案中有一个指向定期更新的浏览器支持图表的链接。

如果您知道确切的位置,那么基于:nth-child()的排除以下所有兄弟姐妹将起作用。

ul li:not(:nth-child(n+3))

这将选择第三个之前的所有lis(例如第一个和第二个)。但是,在我看来,这看起来很难看,而且用例非常紧凑。

您还可以从右到左选择第n个子项:

ul li:nth-child(-n+2)

这也是一样的。

这是类似问题的链接

CSS选择所有以前的兄弟姐妹以获得星级

因此,我使用每个人的回答来发布我的解决方案,任何人都可以将其作为参考,并可能提出改进建议。

//仅用于检查输入值//Consts公司const starRadios=document.querySelectorAll('input[name=“rating”]');//事件监听器starRadios.forEach((radio)=>radio.addEventListener('change',getStarRadioValue));//获取星形无线电值函数getStarRadioValue(事件){警报(event.target.value)//用它做点什么};.星级{字体大小:1.5rem;unicode bidi:bidi覆盖;方向:rtl;文本对齐:左侧;}.star-rating.editable标签:悬停{光标:指针;}.star-rating.editable.icon星形:悬停,.star-rating.editable.icon星形:悬停~.icon星形{背景色:#fb2727!重要的}.icon星形{位置:相对;背景色:#72747d;宽度:32px;高度:32px;显示:内联块;过渡:背景色0.3s;}.icon-star填充{背景色:#fb2727;}.icon星形>标签{显示:内联块;宽度:100%;高度:100%;左:0;顶部:0;位置:绝对;}.icon star>标签>输入[类型=“radio”]{位置:绝对;顶部:0;左:0;变换:translateY(50%)translateX(50%);显示:无;}<div class=“星级可编辑”><span class=“icon star”><标签><input type=“radio”name=“rating”value=“5”/></label></span><span class=“icon star”><标签><input type=“radio”name=“rating”value=“4”/></label></span><span class=“icon star”><标签><input type=“radio”name=“rating”value=“3”/></label></span><span class=“icon star”><标签><input type=“radio”name=“rating”value=“2”/></label></span><span class=“icon star”><标签><input type=“radio”name=“rating”value=“1”/></label></span></div>

我需要一个解决方案来选择上一个兄弟tr。我使用React和Styled组件提出了这个解决方案。这不是我的确切解决方案(这是几小时后的记忆)。我知道setHighlighterRow函数存在缺陷。

OnMouseOver一行会将行索引设置为state,并使用新的背景色重新阅读前一行

class ReactClass extends Component {
  constructor() {
    this.state = {
       highlightRowIndex: null
    }
  }

  setHighlightedRow = (index) => {
    const highlightRowIndex = index === null ? null : index - 1;
    this.setState({highlightRowIndex});
  }

  render() {
    return (
       <Table>
        <Tbody>
           {arr.map((row, index) => {
                const isHighlighted = index === this.state.highlightRowIndex
                return {
                    <Trow 
                        isHighlighted={isHighlighted}
                        onMouseOver={() => this.setHighlightedRow(index)}
                        onMouseOut={() => this.setHighlightedRow(null)}
                        >
                        ...
                    </Trow>
                }
           })}  
        </Tbody>   
       </Table>
    )
  }
}

const Trow = styled.tr`
    & td {
        background-color: ${p => p.isHighlighted ? 'red' : 'white'};
    }

    &:hover {
        background-color: red;
    }
`;

我遇到了同样的问题,当我试图更改输入焦点上的前置图标填充颜色时,我的代码看起来像这样:

<template #append>
    <b-input-group-text><strong class="text-danger">!</strong></b-input-group-text>
</template>
<b-form-input id="password_confirmation" v-model="form.password_confirmation" type="password" placeholder="Repeat password" autocomplete="new-password" />

问题是我使用了一个vue引导槽来注入前缀,所以即使我改变了位置,输入后仍然会呈现

嗯,我的解决方案是滑动它们的位置,并添加自定义前缀和使用的~符号,因为css不支持前一个同级。

<div class="form-input-prepend">
    <svg-vue icon="common.lock" />
</div>
<b-form-input id="password_confirmation" v-model="form.password_confirmation" type="password" placeholder="Repeat password" autocomplete="new-password" />

Scss样式

.form-control:focus ~ .form-input-prepend {
    svg path {
        fill: $accent;
    }
}

因此,只需尝试更改其位置,如果需要,请使用css order或position:absolute;以实现您想要的,并避免使用javascript来满足此类需求。