简体   繁体   English

在jQuery中,如何判断当前对象是否隐藏?

[英]In jQuery, how can I tell if the current object is hidden or not?

Before I call: 在我打电话之前:

$('myObject').show();

I want to know if it is currently hidden or visible. 我想知道它当前是隐藏还是可见。

There's 2 ways to do it, that I know of: 有两种方法可以做到,我知道:

if ($('#something').is(':hidden')) { }

or 要么

if ($('#something').is(':visible')) { }

They should both work. 他们都应该工作。

You can also do something like this: 你也可以这样做:

$('#something:hidden').show();
$('#something:visible').hide();

Which will only call .show() if the item is already hidden, or only call .hide() if the item is already visible. 如果该项目已被隐藏,则仅调用.show();如果该项目已经可见,则仅调用.hide()。

你也可以使用Toggle $(this).toggle();

You can test this with the css() function: 你可以用css()函数来测试它:

if ($('myObject').css('display') == 'none') {
  $('myObject').show();
}

EDIT: 编辑:

Wasn't aware of how cool the :hidden selector is. 不知道:隐藏选择器有多酷。 My suggestion is still useful for testing other attributes, but Alex's suggestion is nicer in this case. 我的建议对于测试其他属性仍然有用,但Alex的建议在这种情况下更好。

From jQuery FAQ : 来自jQuery FAQ

 var isVisible = $('myObject').is(':visible');
 var isHidden = $('myObject').is(':hidden');

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

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