jquery,如何知道 div 是隐藏的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1065147/
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 10:33:01  来源:igfitidea点击:

jquery, how to know div is hidden?

jquery

提问by mamu

I have code that uses jquery.slideup and jquery.slidedown

我有使用 jquery.slideup 和 jquery.slidedown 的代码

How can i know that div is hidden?

我怎么知道 div 是隐藏的?

回答by CMS

To see if an element is visible or not, you can use the visible selectorwith the isfunction:

要查看元素是否可见,您可以使用带有is函数的可见选择器

$("#idElement").is(":visible") // true or false

But sounds to me like you want to toggle the slide effect, for that you can use the slideTogglefunction.

但是在我看来,您想切换幻灯片效果,为此您可以使用slideToggle功能。

回答by Daniel Moura

$('#id').is(':hidden');    //true if is hidden
$('#id').is(':visible');   //true if is visible

But you may want to use slideToggle for your needs.

但是您可能希望使用 slideToggle 来满足您的需要。

回答by JJ.

You can use the visible selector:

您可以使用可见选择器:

http://docs.jquery.com/Selectors/visible

http://docs.jquery.com/Selectors/visible

回答by JJ.

You could use $("#elementID").height() == 0 since you know it is either going to be up or down. It may be faster than doing the .is(":visible") as well but I haven't done any testing on that fact.

你可以使用 $("#elementID").height() == 0 因为你知道它要么上升要么下降。它也可能比执行 .is(":visible") 更快,但我还没有对这个事实进行任何测试。

回答by Zeeshan Akhter

HTML CODE:

HTML代码:

when you click on div id "collapse" if the the div id "flex-container" is visible then its hide and if hide then visible.

当您单击 div id“collapse”时,如果 div id“flex-container”可见,则其隐藏,如果隐藏则可见。

          <div  id="collapse" >collapse</div> 

JQUERY CODE:

查询代码:

 $(document).ready(function() {

   $("#collapse").on('click', function() {

   if($('#flex-container').is(':visible'))
    {  $("#flex-container").hide();  }
    else 
    {  $("#flex-container").show();  } 

   });
 });