Home > Article > Web Front-end > How to use JavaScript's % to change colors on alternate lines
This article mainly shares with you an example of using JavaScript's % to change colors on alternate lines. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to take a look, I hope it can help everyone.
is as follows:
<html> <head> <meta charset="utf-8"> <title>无标题文档</title> <style type="text/css"> li { list-style-type: none; width: 300px; height: 30px; } </style> </head> <body> <ul> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> </ul> <script type="text/javascript"> var oli=document.getElementsByTagName('li'); for(var i=0;i<oli.length;i++) { if(i%2==0) { oli[i].style.background='#F17B7D'; } else { oli[i].style.background='#EDB461'; } } </script> </body> </html>
Have you learned it? Try it yourself.
Related recommendations:
How to calculate the length of an object in JavaScript
Detailed explanation of immediate execution function examples in JavaScript
How to call class in JavaScript
The above is the detailed content of How to use JavaScript's % to change colors on alternate lines. For more information, please follow other related articles on the PHP Chinese website!