JQuery:检查元素是否对用户隐藏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1085457/
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
JQuery: Check whether an Element is Hidden from the user
提问by Graviton
How to check whether a particular element is hidden from the user? In my code, under certain conditions, this code will be called:
如何检查特定元素是否对用户隐藏?在我的代码中,在某些条件下,此代码将被调用:
$("#VersionSelectField").hide('fast');
So I have to make sure that if $("#VersionSelectField")
is hidden, then I would not have to validate the value inside it when I submit the form ( I use JQuery Validate libraryfor this purpose).
所以我必须确保如果$("#VersionSelectField")
是隐藏的,那么我在提交表单时就不必验证其中的值(为此我使用了JQuery 验证库)。
Any ideas?
有任何想法吗?
回答by user120242
$("#VersionSelectField").is(':hidden');
回答by Mark A. Nicolosi
This works for me:
这对我有用:
$("#VersionSelectField").css("display") == "none";
回答by xandy
You may use the callback of the hide() method. For example:
您可以使用 hide() 方法的回调。例如:
$("#VersionSelectField").hide('fast', function() {
GlobalVersionSelectFieldHidden = true;
});
Above is only one method to make use of that, basically the callback will only fires when the animation finished (ie, totally hidden). Of course polluting the global variable scope is very naughty but just a quick example.
以上只是利用它的一种方法,基本上回调只会在动画完成时触发(即完全隐藏)。当然,污染全局变量范围是非常顽皮的,但只是一个简单的例子。
Alternatively, checking whether display is "none" like Mark suggest also works, since the JQ effect will totally hide things using that particular css property.
或者,检查 display 是否为“none”(如 Mark 建议)也有效,因为 JQ 效果将使用该特定 css 属性完全隐藏内容。
回答by Rob
Try $("#versionselectfield[display='none']").length > 0
.
试试$("#versionselectfield[display='none']").length > 0
。