<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>点击将DIV变成红色</title> <style> body{font:12px/1.5 Tahoma;text-align:center;} code,input,button{font-family:inherit;} #div{width: 1000px;height: 240px;position: relative;} #div div{width: 200px;height: 200px;background-color: black;position: relative;float: left;margin: 10px;} button{cursor:pointer;} </style> <script> window.onload = function(){ var oDiv = document.getElementById("div").getElementsByTagName("div"); var oButton = document.getElementsByTagName("button")[0]; oButton.onclick = function(){ for(var i = 0;i<oDiv.length;i++){ oDiv[i].style.backgroundColor = "red"; } }; }; </script> </head> <body> <div id="div"> <div></div> <div></div> <div></div> </div> <p><button>点击将DIV变成红色</button></p> </body> </html>
时间: 2024-10-11 15:51:04