1
A
回答
1
使用jQuery:
$(".my-class").filter(":odd").css("background","red");
$(".my-class").filter(":even").css("background","blue");
.common { padding: 20px 0; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="my-class common"></section>
<div class="">
<section class="my-class common"></section>
</div>
<div class="">
<div>
<section class="my-class common"></section>
</div>
</div>
<section class="my-class common"></section>
4
這是不可能與CSS選擇器。去用JavaScript:
Array.prototype.forEach.call(document.querySelectorAll('.my-class'), function(c, i){
c.style.backgroundColor = i%2 === 0 ? "tomato" : "skyblue";
});
使用jQuery:
$('.my-class:even').css({'background-color': 'tomato'});
$('.my-class:odd').css({'background-color': 'skyblue'});
或者更好的是添加類,使用JavaScript/jQuery的,像my-class--odd
和my-class--even
持有的樣式。
1
有可能僅適用於使用nth-child
CSS和直接子選擇:
body > div,
body > section,
body > section > div {
border: solid 1px green;
margin: 5px;
padding: 5px;
background: #aaa;
}
body > .my-class:nth-child(odd),
body > :nth-child(odd) .my-class {
color: white;
background: red;
}
body > .my-class:nth-child(even),
body > :nth-child(even) .my-class {
color: white;
background: blue;
}
<div class="my-class">.my-class (first)</div>
<div>div
<div class="my-class">.my-class (second)</div>
</div>
<section>section
<div>div
<div class="my-class">.my-class (third)</div>
</div>
</section>
<div class="my-class">.my-class (fourth)</div>
相關問題
- 1. 如何正確選擇每個奇數孩子的第一個孩子?
- 2. 如何在SQLite終端中選擇奇怪的列名?
- 3. 如何從超類中選擇孩子?
- 4. 在jQuery中選擇孩子的孩子
- 5. 根據所有孩子的身體狀況選擇父母
- 6. 如何選擇最後的大孩子?
- 7. 如何選擇沒有firstChild或n-child()的孩子的孩子?
- 8. 如何選擇一個沒有他的孩子的物體?
- 9. 如何點擊奇怪的選擇列表元素?
- 10. Android的奇怪背景ExpandableListView的孩子
- 11. 奇怪顯示的孩子的風格
- 12. 如何檢查哪個孩子選擇
- 13. 如何檢索選擇哪個孩子
- 14. 如何選擇這些孩子div
- 15. jQuery - 如何選擇一個孩子div
- 16. 如何僅在類列表中選擇一個孩子
- 17. 如何選擇很多孩子的一個DIV內
- 18. 如何刪除DOM中最後一個孩子的身體
- 19. Python列表/子列表選擇-1奇怪
- 20. Mysql - 如何選擇所有父母的孩子列表?
- 21. 熊貓:如何選擇任何2列中的[]選擇行
- 22. CSS選擇任何標籤的下一個孩子
- 23. iOS7奇怪的UITableViewCell選擇
- 24. 奇怪的選擇結果
- 25. 如何在QTreeView中選擇孩子的物品複選框
- 26. CSS3:第n個孩子(奇數)選擇器既不選擇列也不行
- 27. 如何用xpath選擇一個內部孩子?
- 28. 奇怪的CSS行爲,對身體
- 29. 身體之間的奇怪聯繫
- 30. 單身物體的奇怪行爲
你不能使用CSS。你需要Javascript。 –