- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <title></title>
- <style>
- li{
- width: 300px;
- height: 30px;
- }
- </style>
- </head>
- <body>
- <ul>
- <li></li>
- <li></li>
- <li></li>
- <li></li>
- <li></li>
- <li></li>
- <li></li>
- <li></li>
- <li></li>
- <li></li>
- </ul>
- <input type="button" value="变色">
- </body>
- <script>
- //先执行这个方法
- changeColor();
- var inps= document.querySelectorAll("input");
- inps[0].οnclick=function(){
- changeColor();
- }
- function changeColor(){
- var lis = document.querySelectorAll("li");
- for(var i = 0; i<10; i++){
- var r = Math.ceil(Math.random()*255);
- var g = Math.ceil(Math.random()*255);
- var b = Math.ceil(Math.random()*255);
- lis[i].style.backgroundColor="rgb("+r+","+g+","+b+")";
- }
- }
- </script>
- </html>