对比.html 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. #box {
  9. width: 200px;
  10. height: 200px;
  11. background: #f00;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="box"></div>
  17. <script>
  18. var box = document.getElementById("box");
  19. //1. 点击事件 this指向点击的对象
  20. box.onclick = function() {
  21. console.log(this); //box
  22. }
  23. // // 2.倒计时 this指向window
  24. // setInterval(function() {
  25. // console.log(this); //
  26. // },1000)
  27. // // 3.对象中的this 指向当前对象
  28. var obj = {
  29. name:"Lucy",
  30. age: 18,
  31. address:function() {
  32. console.log(this);
  33. }
  34. }
  35. obj.address()
  36. // //4.函数中的this 指向window
  37. function fn1() {
  38. console.log(this);
  39. }
  40. fn1();
  41. </script>
  42. </body>
  43. </html>