我有使用BS3模态事件的情况,我的应用程序功能被包含在具有公开方法的对象中(显示模块模式)。我收到了来自BS的事件和我的$ this指向Event对象而不是App对象。 我尝试使用jQuery代理jquery this context,这似乎是最好的,但由于某种原因,这些东西对我来说不起作用
var globalAppDef = (function() {
function modalFilters() {
$('#filtersMore')
.on('show.bs.modal', (event) => {
const sourceElement = $(event.relatedTarget);
$(sourceElement.data().filters).removeClass('hidden');
})
/*
* Transfer the proper @this of the event outside the {globalAppDef} Object
*/
.on('hidden.bs.modal', $.proxy((event) => {
$(this).find(".form-list-items-1").addClass('hidden');
$(this).find(".form-list-items-1").addClass('hidden');
}, this));
}
return modalFilters: modalFilters
}
});
var globalApp = new globalAppDef();
globalApp.initialize();
$(document).ready(function () {globalApp.modalFilters()});
我要实现的是第二个hidden.bs.modal $ this指向我的Modal,即$('#filtersMore')元素。
答案 0 :(得分:0)
实际上它是Arrow函数,因为它保留在Object上下文中。 这种方式有效:
.on('hidden.bs.modal', $.proxy(function (event) {
$(this).find(".form-list-items-1, .form-list-items-2").addClass('hidden');
}, $('#filtersMore')));