简体   繁体   English

选中复选框时样式化复选框同级元素

[英]Styling checkbox sibling element when checkbox is checked

JSX: JSX:

<input 
type="checkbox" 
defaultChecked={myfilter.includes("Banana")}
/>
<span 
    onClick={onClickHandler}
    className="banana">Banana
</span>

Without React:没有反应:

`<input 
    type="checkbox" 
/>
<span 
    className="banana">Banana
</span>`

The goal is to select and style the span element when the input element status is checked.目标是 select 并在检查输入元素状态时设置 span 元素的样式。 The reason why I need to do this way is that the checkbox element is hidden by default.我需要这样做的原因是复选框元素默认是隐藏的。 I tried this, but did not worked:我试过这个,但没有用:

input[type="checkbox"]:checked + :after{
  background: yellow;
}

chamhe :after to span \ chamhe :after span \

input[type="checkbox"]:checked + span{
 background: yellow;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM