( border-top
.alternate
border-bottom
.alternate
), , border-top
:
.alternate
, , :not()
.alternate
, 21 > li >- ,
.alternate
, , .
.alternate
border-top
.alternate
, :first-child
, , .alternate
, border-bottom
.alternate
, :last-child
.
" " .alternate
, , , background
, :nth-of-type()
( :nth-child()
) -. , , .alternate
background
, , JavaScript - CSS, (. ).
(function(){
var wrappers=document.querySelectorAll(".elementWrapper"),
x=wrappers.length,
divs,y,alt;
while(x--){
divs=wrappers[x].querySelectorAll(".alternate");
y=divs.length;
alt=!(y%2);
while(y--)
divs[y].classList.add((alt=!alt)?"odd":"even");
}
})();
.elementWrapper>div:not(.alternate)+div.alternate,
.elementWrapper>div.alternate+div:not(.alternate),
.elementWrapper>div.alternate:first-child{
border-top:1px solid #000;
}
.elementWrapper>div.alternate:last-child{
border-bottom:1px solid #000;
}
.elementWrapper>div.alternate.odd{
background:#ccc;
}
.elementWrapper>div.alternate.even{
background:#eee;
}
.elementWrapper{background:#fff;color:#000;margin:0 0 20px;}.elementWrapper>div{font-family:sans-serif;font-size:14px;overflow:hidden;padding:5px;text-overflow:ellipsis;white-space:nowrap;}
<div class="elementWrapper">
<div>1. Element</div>
<div class="alternate">1. Element with spec. Class</div>
<div class="alternate">2. Element with spec. Class</div>
<div class="alternate">3. Element with spec. Class</div>
<div class="alternate">4. Element with spec. Class</div>
<div class="alternate">5. Element with spec. Class</div>
<div>9. Element</div>
</div>
<div class="elementWrapper">
<div>1. Element</div>
<div>2. Element</div>
<div class="alternate">1. Element with spec. Class</div>
<div class="alternate">2. Element with spec. Class</div>
<div class="alternate">3. Element with spec. Class</div>
<div class="alternate">4. Element with spec. Class</div>
</div>
Hide result