<head> <meta charset="UTF-8"> <title>Title</title> <style> .box{ background-color:red; width: 100px; height: 100px; margin-top: 35px; } </style> </head> <body> <div class="box" > </div> <script> var box=document.getElementsByClassName("box")[0]; box.onclick=function() { box.style.backgroundColor = "blue"; } </script> </body> </html>
两边的backgroundcolor 关键字名字不同
body style 里的写法
<head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div style=" 100px; height: 100px;background-color:pink;">
</div> <script> var div= document.getElementsByTagName("div")[0]; div.onmouseover=function(){ div.style.width="200px"; div.style.height="200px"; div.style.backgroundColor="black"; div.style.opacity="0.2"; div.style.filter="alpha(opacity=20)"; } </script> </body> </html>