これが私のhtmlの短いバージョンです:
<div id="section1" class="section">...</div>
<div id="section2" class="section">...</div>
<div id="section3" class="section">...</div>
<div id="section4" class="section">...</div>
これらのセクションの 1 つが:target
疑似セレクターのターゲットになり、現在、ターゲットのすべての兄弟を選択したいと考えています。1つには、CSSには「すべての兄弟」セレクターがなく、一般的な兄弟コンビネーター~
と隣接する+
兄弟コンビネーターがあることを知っています。
代わりに、擬似セレクターが発生した場合にのみ、CSS が別の要素セット (この場合は兄弟) を取得する方法 (または最も近い方法) があることを確認したいと思います。
.section:target then .section:not(:target){
//apply to all .section not being the target
}
jQuery を使用する場合は、次のようになります。
//if a section was a target
if($('.section:target').length > 0){
//get all sections that are not the target
$('.section:not(:target)').css({
//apply css to them
});
}
ターゲットがある場合にのみスタイルが発生することに注意する必要があります。ターゲットがない場合、スタイルは適用されません。