开发者

Highlight every other list element

开发者 https://www.devze.com 2023-02-20 02:29 出处:网络
I have a div like this: http://jsfiddle.net/ANvpS/. How开发者_Python百科 can I highlight every other list element in the div preferably without using jQuery?The easiest way to perform this is with CSS

I have a div like this: http://jsfiddle.net/ANvpS/. How开发者_Python百科 can I highlight every other list element in the div preferably without using jQuery?


The easiest way to perform this is with CSS, for compatible browsers. Example from this link:

li:nth-child(even) {background: #CCC}
li:nth-child(odd)  {background: #FFF}


http://jsfiddle.net/loktar/ANvpS/2/

var listItems = document.getElementsByTagName('li');

for(var i = 0; i< listItems.length; i+=2){
     listItems[i].style.background='red';   
}
0

精彩评论

暂无评论...
验证码 换一张
取 消