js 中 this 是相对的比如说
<input id="sr1" type="text" onclick="alert(this.id)"/>
<input id="sr2" type="text" onclick="alert(this.id)"/>html 中 记住"万物皆对象"的概念 每一个角标 每一个方法 都是对象
this 在哪个对象中使用 就是谁 在比如说
function aa(){
this.name="ok";
}alert(new aa().name)上面 的this 就代表aa
<input id="sr1" type="text" onclick="alert(this.id)"/>
<input id="sr2" type="text" onclick="alert(this.id)"/>html 中 记住"万物皆对象"的概念 每一个角标 每一个方法 都是对象
this 在哪个对象中使用 就是谁 在比如说
function aa(){
this.name="ok";
}alert(new aa().name)上面 的this 就代表aa
<html>
<script language="javascript">
var dudu=new function(){
this.name='L';
this.grunt=function(){
alert('grunt:'+this.name);
}
this.onerr=function(){
alert('error:'+this.name);
}
}
function onclick()
{
dudu.grunt();
}
window.onerror=dudu.onerr;
</script>
<body>
<input type="text" onClick="onclick();"/>
</body>
</html>
<script language="javascript">
var dudu=new function(){
this.name='L';
this.grunt=function(){
alert('grunt:'+this.name);
}
this.onerr=function(){
alert('error:'+this.name);
}
}
function doclick()
{
dudu.grunt();
}
window.onerror=dudu.onerr;
</script>
<body>
<input type="text" onClick="doclick();"/>
</body>
</html>
alert('grunt:'+dudu.name);
};
this.onerr=function (){
alert('error:'+dudu.name);
}
}
在函数里面用this,this代表着当前函数对象
var dudu={
"name":'L',
"grunt":function(){
alert('grunt:'+this.name);
},
"onerr":function (){
alert('error:'+this.name);
}
}
function onclick1(){
dudu.grunt();
}
window.onerror=dudu.onerr();</script>
方法是那个对象的,属于谁的,那方法的this 是谁的,
不过你要注意,方法的顶层对象.有些方法没有new object ,我们直接 用,
那它的顶层对象就是window ,方法里面的this,就是window,
还有 ojbect.method.call(object2,argusments)
那method里面的this,就只向object2了,
老兄明白了吗
误人子弟啊