如何在 jquery 中检查 div 的显示(无/块)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3776112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:04:58  来源:igfitidea点击:

How to check display (none/block) of a div in jquery?

jqueryhtmlfocusonmouseup

提问by Mubeen

I am using this,

我正在使用这个,

$("#loginanchor1").click(function (e) {
    e.preventDefault();
    $("#signin_menu1").slideDown("slow");
});
$(document).mouseup(function (e) {
    if ($(e.target).parent("a.loginanchor1").length == 0) {
        //$(".signin").removeClass("menu-open");
        $("#signin_menu1").slideUp("slow");
    }
});

Everything works fine but what happens is when the signin_menu1is displayed block and i click my mouse button inside the div the div slidesup... I want mouseup function to be prevented when the signin_menu1is displayed block. So i thought of changing the condition like,

一切正常,但发生的情况是当signin_menu1显示块并且我在 div 内单击鼠标按钮 div 滑动...我希望在signin_menu1显示块时阻止 mouseup 功能。所以我想改变条件,比如,

if(($(e.target).parent("a.loginanchor1").length==0) &&( //check the display of the div)

if(($(e.target).parent("a.loginanchor1").length==0) &&( //check the display of the div)

Now how to check the display?

现在如何检查显示?

回答by Reigel

try

尝试

$(document).mouseup(function (e) {
    var $parent = $(e.target).parent("a.loginanchor1");
    if ($parent.length == 0 && !$("#signin_menu1").is(':visible')) {
        //$(".signin").removeClass("menu-open");
        $("#signin_menu1").slideUp("slow");
    }
});

I'm confused with the problem, but $("#signin_menu1").is(':visible')would check if the div is visible (display:block).

我对这个问题感到困惑,但$("#signin_menu1").is(':visible')会检查 div 是否可见(显示:块)。

added notes:

补充说明:

you may try to check if the $(e.target)is the signin_menu1or the is inside signin_menu1. do it like this,

您可以尝试检查$(e.target)is thesignin_menu1或 is inside signin_menu1。像这样做,

$(document).mouseup(function (e) {
    if ($(e.target).is('#signin_menu1') || $(e.target).closest('#signin_menu1').length > 0) { return ; } // do nothing on mouseup
    var $parent = $(e.target).parent("a.loginanchor1");
    if ($parent.length == 0) {
        //$(".signin").removeClass("menu-open");
        $("#signin_menu1").slideUp("slow");
    }
});

回答by Tim

You can use jquery's :visiblefor this.

您可以为此使用 jquery 的:visible