当我单击 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 .
关于javascript - 即使单击返回顶部按钮也会关闭弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33437487/