<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0011)about:blank -->
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=unicode">
<META content="MSHTML 6.00.2900.2963" name=GENERATOR></HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
window.document.body.attachEvent( 'onclick' ,t ); function t(){ alert( 'this:' + typeof this ); }
alert(typeof t)
alert(typeof window.document.body)
//-->
</SCRIPT>
</BODY></HTML>
<!-- saved from url=(0011)about:blank -->
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=unicode">
<META content="MSHTML 6.00.2900.2963" name=GENERATOR></HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
window.document.body.attachEvent( 'onclick' ,t ); function t(){ alert( 'this:' + typeof this ); }
alert(typeof t)
alert(typeof window.document.body)
//-->
</SCRIPT>
</BODY></HTML>
我感觉应该是window对象,从下面的代码中可以看出
<input name=o>
<script language=javascript>
o.attachEvent( 'onclick' ,t ); function t(){ for(var i in this){alert(i)}alert( 'this:' + this ); }
</script>
<input name=o>
<script language=javascript>
a=document.getElementById("o")
o.attachEvent( 'onclick' ,t ); function t(){alert( 'this:' + a.tagName ); }
</script>
<title>get this</title>
<script type="text/javascript">
function t()
{
alert('this'+this);
}window.onload=function(){
var o=document.getElementById('o');
//o.attachEvent('onclick',t);
o.addEventListener('click',t,false);
}
</script>
</head><body>
<input type="button" id="o" value="click" />
</body></html>alert出“this[object HTMLInputElement]”
因为是window对象,最好用两个window对象更清晰一些
<input name=o>
<script language=javascript>
o.attachEvent( 'onclick' ,t ); function t(){this.onload=function(){alert('hehe')} }
t()
</script>
<!-- saved from url=(0011)about:blank -->
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=unicode">
<META content="MSHTML 6.00.2900.2963" name=GENERATOR></HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
window.document.body.attachEvent( 'onclick' ,t ); function t(){ alert( 'this:' + typeof this ); alert(this==top)}
alert(typeof t)
alert(typeof window.document.body)
//-->
</SCRIPT>
</BODY></HTML>
document.getElementById 、 document.getElementsByName
在function t中如何得到 o 和 t对象(函数)本身呢?
o.onclick= function(){ alert( 'this:' + this ); }
<script language=javascript>
o.attachEvent( 'onclick' ,t ); function t(){alert(this==window) }
</script>
在function t中如何得到 o 和 t对象(函数)本身呢?
---------------------
<input name=o>
<script language=javascript>
o.onclick=function(){
alert(this.tagName);
}
</script>
o.attachEvent( 'onclick' ,t );
怎么在t里得到o对象?
o.attachEvent( 'onclick' ,function(){t()} );
它只是在o的onclick事件中调用了t(),而不是copy函数t给o对象.所以alert出来的this实际上是window对象
<body>
<input type="button" id="bt" value="OK" />
<SCRIPT LANGUAGE="JavaScript">
<!--
var bt = document.getElementById("bt");
bt.attachEvent("onclick", t);
function t()
{
alert(this.location); //很明显,这里的 this 被指向了 window 对象
}
//-->
</SCRIPT>
</body>
</html>在JS里那些无主的 this 都是暗指向 window 对象了。<html>
<body>
<input type="button" id="bt" value="OK" />
<SCRIPT LANGUAGE="JavaScript">
<!--
var bt = document.getElementById("bt");
bt.attachEvent("onclick", t);
function t(e)
{
e = window.event || e;
var obj = e.srcElement || e.target; //这样的写法可兼容所有浏览器
alert(obj.value);
}
//-->
</SCRIPT>
</body>
</html>
function t()
{
alert(this);
}
这样写 这里的this就是o。。
function t(e)
{
e = window.event || e;
var obj = e.srcElement || e.target; //这样的写法可兼容所有浏览器
alert(obj.value);
}
click之后那个形式参数e是什么啊?
只有attachEvent方法加入后在触发才有e么?
哈..不过是通过这个帖子学的兼容..event