我有這段代碼:我需要訪問事件上下文和對象範圍內的事件處理程序
function ActivityDialog(_divId, _title) {
function addButton() {
var buttonElement = document.createElement('input');
buttonElement.setAttribute('type','button');
buttonElement.setAttribute('class','button');
buttonElement.setAttribute('id','updateButton-' + id););
buttonElement.onclick = this.updateAction;
};
function updateAction() {
var buttonId = this.id; // correct: this is the Button
this.sendUpdateRequest(stringUrl); // not defined: Need to reference the current ActivityDialog!!!
};
function sendUpdateRequest(url) {
// something...
};
}
正如你所看到的問題是,當我打電話功能sendUpdateRequest;
哪能,在同一時間,檢索按鈕信息並調用函數?
http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript我希望這篇文章能幫到 – 2011-12-22 15:39:28
我不明白這個問題。 – 2011-12-22 15:45:17
@ TomalakGeret'kal他有一個範圍問題,他試圖在事件處理程序中用'this'(Button)和Object context(ActivityDialog)引用事件上下文。 – jondavidjohn 2011-12-22 16:34:28