20.this指向.html 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. #box1 {
  9. width: 300px;
  10. height: 300px;
  11. background-color: aqua;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="box1"></div>
  17. <script>
  18. var box1 = document.getElementById("box1");
  19. // 1.点击事件 this指向当前点击元素
  20. // box1.onclick = function() {
  21. // console.log(this);
  22. // }
  23. // 2.定时器 this指向window
  24. // var timer = setInterval(function(){
  25. // // console.log(this);
  26. // box1.onclick = function() {
  27. // console.log(this);
  28. // }
  29. // },1000)
  30. // 3.对象 this指向当前对象
  31. // var obj = {
  32. // name:"LiLi",
  33. // age: 10,
  34. // eat:function() {
  35. // console.log(this);
  36. // }
  37. // }
  38. // obj.eat();
  39. // 4.函数 this指向window
  40. function fn1() {
  41. console.log(this)
  42. }
  43. fn1()
  44. </script>
  45. </body>
  46. </html>