web.js.this详解

来源:互联网 发布:js dom编程艺术 pdf 编辑:程序博客网 时间:2024/06/05 19:26

用this,记住一点,当前的方法属于谁,this就表示谁!!!!
看如下例子

<!DOCTYPE html><html>    <head>        <meta charset="UTF-8">        <title>输出空格</title>    </head>        <script type="text/javascript">        window.onload=function(){        var a=[1,2];        var obnt=document.getElementById('bnt');        a.b=5;        a.Show=function(){             alert(this.b)        }        obnt.onclick=function(){            alert(this.value)        }        a.Show();        }        </script>        <body>        <input type="button" name="bnt" id="bnt" value="你好" />          </body></html>

第一个是自己定义的show方法,show属于a,所以this就表示a;
即弹出(a.b=5)5。
第二个.onclick方法,它属于obnt,所以this就表示obnt,
即obnt.value

0 0