日期:2014-05-16  浏览次数:20387 次

看犀牛书遇到的this困惑,请高手讲解。
书上说:
In   the   original   Level   0   event   model,   when   a   function   is   registered   as   an   event   handler   for   a   document   element,   it   becomes   a   method   of   that   document   element   (as   discussed   previously   in   Section   17.1.5.).   When   the   event   handler   is   invoked,   it   is   invoked   as   a   method   of   the   element,   and,   within   the   function,   the   this   keyword   refers   to   the   element   on   which   the   event   occurred.

但是下面的情况却不太符合:
<div   id= "elmtDiv "   onclick= "EventHandler(); "> division   element </div>
<script   language= "javascript ">
function   EventHandler(){
        //在此使用this,this引用的是window对象。
}
</script>
是这种写法不属于Level   0的事件模型吗?请高手指点。


------解决方案--------------------
<div id= "elmtDiv " onclick= "EventHandler(); "> division element </div>
仅仅是方法调用,而非事件注册!

以下写法才是,L@_@K

<body>
<div id= "elmtDiv "> division element </div>
<script type= "text/javascript ">
<!--
function EventHandler(){
//在此使用this,this引用的是window对象。
alert(this.tagName);
}
document.getElementById( "elmtDiv ").onclick = EventHandler;
//-->
</script>
</body>
------解决方案--------------------
补充一点onclick= "EventHandler(); "
在被解释的时候被解释成 HTMLElement.onclick=new Function( "EventHandler(); ");