javascript - Alternating background color in HTML UL -
i'd alternate background color in html un-ordered list without using jquery.
best option css3 :nth-child
selector:
#myul li:nth-child(odd) { background-color: red; } #myul li:nth-child(even) { background-color: green; }
or if reason need javascript solution:
<style> li.even { background-color: red; } li.odd { background-color: blue; } </style> var ul = document.getelementbyid('myul'); var items = ul.getelementsbytagname('li'); (var = 0; < items.length; i++) { var class = % 2 == 0 ? 'even' : 'odd'; items[i].classname = class; }
or mentioned generate classes server-side.
Comments
Post a Comment