【发布时间】:2013-03-27 01:41:44
【问题描述】:
我有一个 HTML 菜单选项,我在 jQuery 中绑定了一个 click 处理程序:
var xyz = {
getMainContainerSelector: function () {
return '.container#main';
},
bindMenuOptions: function () {
$('#menu_outcome_list').bind('click', function() {
// inject template
$(this.getMainContainerSelector()).html(ich.outcomeListTemplate({}));
// load datatable
$('#outcomes').dataTable({
"bServerSide": true,
'sPaginationType': 'bootstrap',
"sAjaxSource": '../php/client/json.php?type=outcomes'
});
});
},
...
}
下面这行有问题:
$(this.getMainContainerSelector()).html(ich.outcomeListTemplate({}));
我猜这是一个上下文问题。我的意思是,在绑定函数内部,this 不再是xyz,而是 ('#menu_outcome_list') HTML 元素。我想做的只是从绑定函数内部调用xyz的方法。
【问题讨论】:
-
xyz.getMainContainerSelector() 不起作用吗?
-
旁注,使用
.container#main没有多大意义,直接使用#main -
@KevinB,好吧,你是对的 ;)
标签: jquery binding jquery-context