我有这个 javascript 对象:
StoryGroup = {
groupInput: '.story-group-container input[type="text"]',
container: '.checkbox-container',
submit: '.checkbox-container .filter',
body: 'body',
init: function() {
$(this.groupInput).click(this.showForm.bind(this));
$(this.body).click(this.hideForm.bind(this));
},
showForm: function() {
$(this.container).show();
},
hideForm: function(e) {
if (e.currentTarget == $(this.groupInput) || e.currentTarget == $(this.container)) {
$(this.container).show();
} else {
$(this.container).hide();
}
}
}
为什么这不能正常工作?如果我删除“hideForm”功能。 showForm 函数工作正常,并在单击 groupInput 时显示我的“容器”。我想在单击主体上的任何位置时运行 hideForm 函数,除非该单击是在输入或容器上。这就是为什么我传入“e”以确保点击不在这两个元素上。
请您参考如下方法:
您正在检查引用文献,显然在您的情况下两者不会相同,
hideForm: function(e) {
if (!($(e.currentTarget).is(this.groupInput) || $(e.currentTarget).is(this.container))) {
$(this.container).hide();
}
}
尝试在此上下文中使用.is(selector)
。