JavaScript DOM操作案例div的高亮显示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style> * { margin: 0; padding: 0; } div { width: 100px; height: 100px; background-color: green; float: left; margin-left: 10px; border: 5px solid green; } </style> </head> <body> <div></div> <div></div> <div></div> <div></div> <div></div> <script> //获取所有的div标签 var divObjs = document.getElementsByTagName("div"); for (var i = 0; i < divObjs.length; i++) { //为每个div添加鼠标进入的事件 divObjs[i].onmouseover = function () { this.style.border = "5px solid red"; }; //为每个div添加鼠标离开的事件 divObjs[i].onmouseout = function () { this.style.border = ""; }; } </script> </body> </html>