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

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -