function test(){
....
}
test.prototype.a()= function(){
 var container = $("#table");
 container.append("<span onclick='在这里如何写b方法'></span>");
}test..prototype.b()= function(){
...
}
第6行该怎么写呢,在a方法中调用b方法是用this.b(),但我现在是在append后的html中,onclick后才调用,如果直接写成onclick='+ this.b() +';这样不用点击,直接就运行了,是不对的,该怎么写呢,我现在是用实例名写死的,如
var mytest=new test();
onclick就写死了:
onclick="mytest.b()";,但这样写死了不太好吧,如果非要写死的话,请问在a方法中,如何得到实例的名称,也就是mytest

解决方案 »

  1.   


    var container = $("#table");
     container.append("<span></span>");
    $("#table span").click(function(){//你改一下选择器,看怎样能匹配到刚才添加的span
        //这里写方法
    });
      

  2.   


    // 换个思维 把主导元素放到前面
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="jquery-1.3.2.js"></script>
    <title>无标题文档</title>
    </head><body>
    <div id="table" style="width:100px;height:100px;border:1px solid red"></div>
    <script type="text/javascript">
    <!--
    function test(){}
    test.prototype.a= function(){
    var _this = this;
    var container = $("#table");
     $("<span style='width:30px;height:20px;background-color:blue'>Span</span>").appendTo(container).click(function(){
    _this.b();
     });
    }test.prototype.b= function(){
    alert('b方法');
    }
    var T = new test();
    T.a();
    //-->
    </script>
    </body>
    </html>