for和getElementByTagName配合
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>for和getElementByTagName配合</title> <style type="text/css"> div{width:100px;height: 100px;border:1px solid black;} </style> <script type="text/javascript"> window.onload=function(){ var aDiv=document.getElementsByTagName('div'); var i=0; for(i=0;i<aDiv.length;i++){ aDiv[i].style.background='red'; } } </script> </head> <body> <div></div> <div></div> <div></div> </body> </html>