onclick和对象的身份

时间:2013-09-30 17:28:14

标签: javascript onclick prototype

我希望这仍然有意义。我没有代码,但我想知道如何做到这一点。问题出在“onclick=\"thatThing.doSmthg();\"”。

导致“未定义theThing”。

谢谢!

var oneThing = new Thing();

letsDoSmthg(oneThing);

letsDoSmthg(thatThing){
    document.getElementById("fezok").innerHTML+="<input type=\"button\" value=\"Do smthg\" onclick=\"thatThing.doSmthg();\">";
}

function Thing(){
    this.variable1=0;
}
Thing.prototype={
    doSmthg: function(){
        this.variable1=1534;
    },

1 个答案:

答案 0 :(得分:1)

不要使用字符串来创建HTML。使用元素。

var letsDoSmthg = function(thatThing) {
    var button = document.createElement('input');
    button.type = 'button';
    button.onclick = function() {
        thatThing.doSmthg();
    };

    document.getElementById("fezok").appendChild(button);
};