Javascript CSS中是否有一种方法可以访问元素中的元素。第一要素,第二要素?
假设我有Javascript CSS中是否有一种方法可以访问元素中的元素。第一要素,第二要素?,javascript,html,css,Javascript,Html,Css,假设我有 <div class='r1'> <div> Apple </div> <div> Banana </div> <div> Orange </div> <div> Tulip </div> </div> .ri:n子项(1){ 颜色:红色;
<div class='r1'>
<div> Apple </div>
<div> Banana </div>
<div> Orange </div>
<div> Tulip </div>
</div>
.ri:n子项(1){
颜色:红色;
}
.ri:n个孩子(2){
颜色:蓝色;
}
等等。您可以使用
:nth-child()
选择器。例如:
.r1:nth-child(1) {
color: red;
}
.r1:nth-child(2) {
color: blue;
}
你也可以使用一些很酷的配方食品,比如:
/* Will make every 5th element red, starting at the second one */
.r1:nth-child(5n+1) {
color: red;
}
/* Will make the odd div elements blue */
.r1:nth-child(odd) div {
color: blue;
}
了解第N个孩子你是说div。。。
/* Will make every 5th element red, starting at the second one */
.r1:nth-child(5n+1) {
color: red;
}
/* Will make the odd div elements blue */
.r1:nth-child(odd) div {
color: blue;
}