12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <!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: red;
- }
- </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)
- // },1000)
- //3.对象下面 this指向对象本身
- // var person = {
- // name: 'zs',
- // age: 20,
- // eat: function(){
- // console.log(this)
- // }
- // }
- // person.eat()
-
- //4.函数内 this指向window
- function myFun(){
- console.log(this)
- }
- myFun()
- </script>
- </body>
- </html>
|