除非特定元素,否则通过单击 body 隐藏元素

Hide element by clicking body unless specific element

我有这个 javascript object:

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 时显示我的 'container'。我想 运行 单击 body 上的任何位置时的 hideForm 函数,除非该单击是在输入或容器上。这就是为什么我传入 'e' 以确保点击不在这两个元素上。

您正在检查参考资料,显然在您的情况下两者不会相同,

hideForm: function(e) {
   if (!($(e.currentTarget).is(this.groupInput) || $(e.currentTarget).is(this.container))) {
      $(this.container).hide();
   }
}

在此上下文中尝试使用 .is(selector)