<!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>

解决方案 »

  1.   

    这样完全不能算答案吧 如果我的onck函数是要把自身这个按钮移除呢this.elbutton.parentNode.removeChild(this.elbutton);你这样相当于重新定义了一个与类无关的变量啊
      

  2.   

    你不是问我 为什么显示undifined吗?
    this作用于只是在函数范围内
    this.elbutton.onclick=this.onck;调用a()this.onck=function(){
    //this.zzy=200;如果不定义,外面的zzy进不来所以就undifined
    alert(this.zzy);
    }
      

  3.   

    算了 我就这样问吧 function a(){
    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;
    }
      

  4.   

    作用区域没搞清楚
    <!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>
      

  5.   

    是不是只能用 tmp.al() 这样从外部调用呢
      

  6.   

    呃 可能是你javascript过关了 但是语文没过关...
    哈哈 抱歉 害你解释半天 不过我也给了你10分啦