123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- #box1 {
- width: 200px;
- height: 200px;
- background: #f00;
- }
- </style>
- </head>
- <body>
- <div id="box1"></div>
- <script>
- var box1 = document.getElementById("box1");
- // 1.点击事件 this指向对象本身
- // box1.onclick = function() {
- // console.log(this)
- // }
- // 2.定时器 指向全局window
- box1.onclick = function() {
- setInterval(function() {
- console.log(this)
- },1000)
- }
- // 3.对象中的this 指向对象本身
- var obj = {
- name:"Lucy",
- age:18,
- address:function() {
- console.log(this)
- }
- }
- obj.address()
- // 4.函数中this 指向window
- function fn1() {
- console.log(this,'函数')
- }
- fn1()
- </script>
- </body>
- </html>
|