本帖最后由 ffxiangyu 于 2011-09-19 12:15:56 编辑

解决方案 »

  1.   

               alert(this.num);改了, 此this 非彼this
      

  2.   

    去掉this,里面的this和外面的this的作用域是不同的,但是num是可以传进去的
      

  3.   

    事件处理函数里面的this和类里面的this是不一样滴,同学。。<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <div id="o1" style='width:100px; height:50px; border:10px solid black'> </div>
    <div id="o2" style='width:100px; height:50px; border:10px solid black; margin-top:3px'> </div>
    <script>Test = function(obj, num) {
        var _this = this;
        this.obj = obj;
        this.num = num;
        this.obj.addEventListener('mousedown', 
            function(event) {
                alert(_this.num);
            },
            false
        );
    };var obj1 = new Test(document.getElementById("o1"), 1);
    var obj2 = new Test(document.getElementById("o2"), 2);
    </script>
    </body>
    </html>
      

  4.   


    我试了一下,调用方法可以这样实现,谢谢<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <div id="o1" style='width:100px; height:50px; border:10px solid black'> </div>
    <div id="o2" style='width:100px; height:50px; border:10px solid black; margin-top:3px'> </div>
    <script>Test = function(obj, num) {
    this.obj = obj;
    this.num = num; var _this = this;

    this.showNum = function() {
    alert(_this.num);
    };

    this.obj.addEventListener('mousedown', 
    this.showNum,
    false
    );
    };var obj1 = new Test(document.getElementById("o1"), 1);
    var obj2 = new Test(document.getElementById("o2"), 2);
    </script>
    </body>
    </html>