我有这段代码:我需要访问事件上下文和对象范围内的事件处理程序
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