this的应用

 1 <!DOCTYPE HTML>
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 5 <title>无标题文档</title>
 6 
 7 <script>
 8 
 9 /*
10 alert(this);            window
11 
12 fn1(this);
13 function fn1(obj){
14     obj => window
15 }
16 
17 oDiv.onclick = function (){
18     this
19     fn1(this);
20 };
21 function fn1(obj){  obj => oDiv }
22 */
23 
24 window.onload = function (){
25     var aBtn = document.getElementsByTagName('input');
26     var that = null;            //
27     
28     for(var i=0; i<aBtn.length; i++){
29         /*
30         aBtn[i].onclick = function (){
31             // this.style.background = 'yellow';
32             
33             that = this;
34             
35             fn1();
36         };
37         */
38         aBtn[i].onclick = fn1;
39     }
40     
41     function fn1(){
42         // this        =>  window
43         // that.style.background = 'yellow';
44         
45         // this.style.background = 'red';
46     }
47 };
48 </script>
49 
50 </head>
51 
52 <body>
53 
54 <input type="button" value="按钮1" />
55 <input type="button" value="按钮2" />
56 <input type="button" value="按钮3" />
57 
58 </body>
59 </html>

 

posted on 2016-03-09 15:51  hduhdc  阅读(151)  评论(0编辑  收藏  举报