1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <!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>
- #box {
- width: 200px;
- height: 200px;
- background: #00f;
- }
- </style>
- </head>
- <body>
- <div id="box"></div>
- <script>
- var box = document.getElementById("box");
- // 1.点击事件 this指向点击对象
- // box.onclick = function() {
- // console.log(this);
- // }
- // 2.定时器 指向window
- // setInterval(function() {
- // console.log(this)
- // },1000)
- // window.onclick = function() {
- // setInterval(()=>{
- // console.log(this)
- // },2000)
- // }
- // box.onclick = function() {
- // setInterval(()=>{
- // console.log(this)
- // },2000)
- // }
- // 3.对象中 指向当前对象
- // var obj = {
- // name:"Lucy",
- // age:10,
- // address:function() {
- // console.log(this);
- // }
- // }
- // obj.address();
- // 4.函数中 this执行window
- function fn1() {
- console.log(this);
- }
- fn1();
- </script>
- </body>
- </html>
|