本文重点解决jsDom为页面中的元素绑定键盘或鼠标事件问题,希望能够帮助到你
html鼠标事件
onload 页面加载
onclick 鼠标单击
onmouseover 鼠标移入
onmouseout 鼠标移出
onfocus 获取焦点
onblur 失去焦点
onchange 域的内容改变
在事件触发中,this表示对当前dom对象的引用
1、html事件,在html元素上直接绑定事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .btn{ width:140px; height:30px; background:#abcdef; line-height:30px; text-align: center; font-size:14px; border-radius:5px; cursor:pointer; } div{ width:140px; height:140px; background:#abcdef; line-height:140px; text-align: center; font-size:14px; margin:50px 0; } </style> </head> <body> <button id="btn" class="btn" onclick="alert('我被点击啦!');">我是按钮</button> <div onmouseover="myFun(this,'orange')" onmouseout="myFun(this,'pink')">我是div</div> <script> function myFun(obj,bgcolor){ obj.style.backgroundColor=bgcolor; } </script> </body> </html>
本网刊登的文章均仅代表作者个人观点,并不代表本网立场。文中的论述和观点,敬请读者注意判断。