<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> test</title>
<script>
function a(){
this.zzy;
this.elbutton;
this.onck=function(){
this.zzy=200;//作用域(局部与全局)
alert(this.zzy);
}
this.addbutton=function(place){
place.appendChild(this.elbutton);
}
this.zzy=100;
this.elbutton=document.createElement('button');
this.elbutton.innerHTML='xx';
this.elbutton.onclick=this.onck;}
</script>
</head>
<body id="body1">
<script>
var tmp=new a;
var body=document.getElementById('body1');
tmp.addbutton(body);
</script>
</body>
</html>
this作用于只是在函数范围内
this.elbutton.onclick=this.onck;调用a()this.onck=function(){
//this.zzy=200;如果不定义,外面的zzy进不来所以就undifined
alert(this.zzy);
}
this.zzy;
this.elbutton;
this.onck=function(){
this.al();// 我怎么样可以调用到下面那个al呢}
this.addbutton=function(place){
place.appendChild(this.elbutton);
}
this.al=function(){// 就是这个
alert('al');
}
this.zzy=100;
this.elbutton=document.createElement('button');
this.elbutton.innerHTML='xx';
this.elbutton.onclick=this.onck;
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> test</title>
<script>
function a(){
var PackUp=this;//闭包对象
this.zzy;
this.elbutton;
this.onck=function(){
alert(PackUp.zzy);
//alert(this.zzy);//此时this==button对象,button无zzy
}
this.addbutton=function(place){
place.appendChild(this.elbutton);
}
this.zzy=100;
this.elbutton=document.createElement('button');
this.elbutton.innerHTML='xx';
this.elbutton.onclick=this.onck;}
</script>
</head>
<body id="body1">
<script>
var tmp=new a;
var body=document.getElementById('body1');
tmp.addbutton(body);
</script>
</body>
</html>
哈哈 抱歉 害你解释半天 不过我也给了你10分啦