我有以下標記,並且我需要找到一種CSS方式來在每次.hide類處於活動狀態時更改三角形。CSS:之前和兄弟選擇器不工作
要做到這一點我用一個僞元素是這樣的:
summary:before {
content: '\25BC';
}
summary:before + .hide {
content: '\25BA';
}
的問題是:這是行不通的。箭頭不會改變。儘管總結:之前+ .hide出現在DevTools中。
那麼如何在不使用JavaScript的情況下達到預期的效果CSS?
var $div = $('summary');
$div.on('click', function() {
$('ul').toggleClass('hide');
});
.wrapper {
width: 300px;
height: 300px;
background-color: #ecf0f1;
}
.details {
outline: none;
background-color: #95a5a6;
cursor: pointer;
position: relative;
}
summary {
outline: none;
margin-left: 30px;
}
summary:before {
content: '\25BA';
font-size: 12px;
position: absolute;
top: 2px;
left: 13px;
}
summary:before + ul.hide {
content: '\25BC';
font-size: 12px;
position: absolute;
top: 2px;
left: 13px;
}
.hide {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
<div class="details">
<summary>Sample</summary>
<ul class="hide">
<li>
<input type="radio" checked/>
<label>Label 1</label>
</li>
<li>
<input type="radio" />
<label>Label 2</label>
</li>
<li>
<input type="radio" />
<label>Label 3</label>
</li>
</ul>
</div>
</div>
的可能的複製[是否有一個 「前一個兄弟」 CSS選擇器?(http://stackoverflow.com/questions/1817792/is-there-a-previous-sibling-css-選擇器) – Pete
「總結:在+ .hide之前出現在DevTools中」等等,認真嗎?這太奇怪了。這不是一個有效的選擇器。 – BoltClock
如果您使用jQuery切換類,爲什麼不在父容器上執行它,或者只是在摘要上切換一個類? – Pete