对象的单击和标识

onclick and identity of an object

本文关键字:标识 单击 对象      更新时间:2023-09-26

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

它导致"thatThing is not defined"。

谢谢!

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;
    },

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

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