3_绑定事件.html 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. div{
  9. width: 200px;
  10. height: 100px;
  11. background-color: red;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div>按钮</div>
  17. <script>
  18. // var arr = [1];
  19. // var a = 1 + arr[0];
  20. // var oBtn = document.getElementsByTagName("div");
  21. // oBtn = oBtn[0];
  22. var oBtn = document.getElementsByTagName("div")[0];
  23. // console.log(oBtn);
  24. // 鼠标点击 左键
  25. // oBtn.onclick = function(){
  26. // console.log("hello world");
  27. // }
  28. // 鼠标右键
  29. // oBtn.oncontextmenu = function(){
  30. // }
  31. // 鼠标移动
  32. // oBtn.onmousemove = function(){
  33. // console.log("onmousemove")
  34. // }
  35. // 鼠标移入
  36. oBtn.onmouseover = function(e){
  37. console.log("onmouseover");
  38. console.log(e);
  39. console.log(this);
  40. }
  41. var obj = {
  42. name: "张三",
  43. age: 18,
  44. talk:function(){
  45. console.log(obj.name);
  46. console.log(this.name);
  47. }
  48. }
  49. obj.talk();
  50. </script>
  51. </body>
  52. </html>