我有一些无线电选项,我想要显示/隐藏类。我有一些有这些类的div。有没有办法使用CSS来显示/隐藏不是兄弟姐妹的元素?
HTML:
<input type="radio" class="one radioselect" value="one" id="one" name="opts" /> <label for="one" class="select radlabel">one</label>
<input type="radio" class="two radioselect" value="two" id="two" name="opts" /> <label for="two" class="select radlabel">two</label>
<div class="one">abc</div>
<div class="two">def</div>
CSS:
input.one:checked ~ a.one { display:inline; }
input.two:checked ~ a.two { display:inline; }
input.one:checked ~ a:not(.one) { display:none; }
input.two:checked ~ a:not(.two) { display:none; }
现在我的标记必须是这样的:
<div class="row">
<input type="radio" class="one radioselect" value="one" id="one" name="opts" /> <label for="one" class="select radlabel">one</label>
<input type="radio" class="two radioselect" value="two" id="two" name="opts" /> <label for="two" class="select radlabel">two</label>
</div>
<div class="one">abc</div>
<div class="two">def</div>
新的标记意味着输入不再是兄妹的div和CSS不再适用(据我所知)。 我已经尝试了所有我能想到的选择器组合,也许这对于纯CSS来说是不可能的。我的目标是避免JavaScript可以在CSS中工作。如果这是一个解决方案,我愿意研究SASS/LESS。
不可能使用您指定的HTML。单选按钮是否可见?如果不是的话,你可以把它们放在'.row'之外(*和以前一样*),并将'label'元素保存在'.row'中。 –
这可能工作。我会尽快回复您,但如果确实有效,我无法选择您的评论作为答案。 – RHebel