即使单击返回顶部按钮,也会弹出关闭

Popup closing even when clicked on back to top button

本文关键字:出关 按钮 单击 返回 顶部      更新时间:2023-09-26

当我点击一个div时,会打开一个弹出窗口,当我点击弹出窗口之外的任何地方时,它都会关闭。因为这个功能,即使当我点击"返回顶部按钮"时,弹出窗口也会关闭,这是我不想要的。我希望弹出窗口在外部点击时关闭,但在点击几个元素时,我希望弹出菜单保持打开。

我的JS供参考:

$(document).click(function (e) {
    if (!$(e.target).is('#myPanel, #myPanel*')) {
        $("#myPanel").hide();
        $(".span10").width(600);
    }
});

你走对了。但是,您需要在逗号分隔的选择器中包含不应关闭弹出窗口的所有元素。此外,对于那些具有子元素的元素,您需要包括ID,后面跟着一个星号,但用空格分隔。空格是代码中缺少的内容。它应该是#myPanel *而不是#myPanel

$(document).click(function (e) {
    //Do not close popup for these:
    // - Element with ID myPanel                      #myPanel
    // - Any descendants of element with ID myPanel   #myPanel *
    // - Element with ID backtotop                    #backtotop
    if (!$(e.target).is("#myPanel, #myPanel *, #backtotop")) {
        $("#myPanel").hide();
        $(".span10").width(600);
    }
});

JSFiddle。