繁体   English   中英

当用户单击DIV而不是滚动条时,jQuery隐藏DIV

[英]jQuery to hide a DIV when the user clicks outside of it, but not on scrollbar

我已经阅读了这篇文章,并且正在使用以下代码:

    $(document).mouseup(function (e)
{
    var container = $("YOUR CONTAINER SELECTOR");

    if (!container.is(e.target) // if the target of the click isn't the container...
        && container.has(e.target).length === 0) // ... nor a descendant of the container
    {
        container.hide();
    }
});

此代码的问题是,如果我单击滚动条,将关闭div!

建议?

请改用mousedown事件。

$(document).mousedown(function (e) {
   //same code here
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM