匿名函数和鼠标移入移除事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>改变边框</title> <style> .def { border: 1px solid #ccc; margin-right: 120px; } .hover { border: 1px solid red; margin-right: 120px; } </style> <script> // function over(target) { // target.className = 'hover'; // } // function def(target) { // target.className = 'def'; // } window.onload=function () { var img=document.getElementsByTagName('img');//取得所有的图片数组 for(var i=0;i<img.length;i++){//遍历每一张图片 img[i].onmouseover=function(){//每一张图片加一个onmouseover事件 this.className='hover'; } img[i].onmouseout=function () { this.className='def'; } } } </script> </head> <body "> <table> <tr> <td><img src="images/new_01.jpg" alt="" class="def"></td> <td><img src="images/new_02.jpg" alt="" class="def"></td> <td><img src="images/new_03.jpg" alt="" class="def"></td> </tr> </table> </body> </html>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步