在下面的代碼中,當我從對象外部調用「some_function」時,它會提示零。 當我點擊「some_button」時,也會調用「some_function」,但在這種情況下它會提示未定義。 什麼問題?對象內部按鈕的點擊事件
some_object = {
some_variable: 0,
some_button: $('<input />', {
type: "button",
value: "Click Me"
}),
some_function: function() {
alert(this.some_variable);
},
init: function() {
$("body").append(this.some_button);
this.some_button.click(this.some_function); // Result is undefined
}
}
some_object.init();
some_object.some_function(); // Result is 0
some_object = { some_variable:1, some_button:$('',{ type:「button」, value:「Click Me」 }), some_function:function(){ alert(some_object.some_variable);初始化:function(){ $(「body」)。append(this.some_button); $ this = this; $ this.some_button.click(function(){ $ this.some_function(); }); } } some_object.init(); some_object.some_function(); –