1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- #div1 {
- width: 200px;
- height: 200px;
- background: pink;
- }
- </style>
- </head>
- <body>
- <div id="div1"></div>
- <script>
- var div1 = document.getElementById('div1')
- //1.当前对象引用中, 谁的事件 this指向就是谁
- // div1.onclick = function(){
- // console.log(this)
- // }
- //2.在定时器当中, this指向window
- // var timer = setInterval(function () {
- // console.log(this)
- // }, 2000)
- // div1.onclick = function () {
- // var timer = setInterval(function () {
- // console.log(this)
- // }, 2000)
- // }
- //3.在对象下面 this指向对象本身
- // var person = {
- // name: 'zs',
- // age: 20,
- // eat: function(){
- // console.log(this)
- // }
- // }
- // person.eat()
- //4.在函数内 this指向window
- // function xx(){
- // console.log(this)
- // }
- // xx()
- div1.onclick = function(){
- console.log(this,'a')
- var timer = setInterval(function(){
- console.log(this,'b')
- },2000)
- abc()
-
- }
- var abc = function(){
- console.log(this,'c')
- }
- console.log(this,'d')
- /*
- div a d window
- window b a div
- window c c window
- window d b window
-
- */
- </script>
- </body>
- </html>
|