JQuery - 如果 DIV ID 可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1457038/
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 - If DIV ID is Visible
提问by RobertC
I am trying to find out of a DIV is hidden or if it is exposed.
我试图找出一个 DIV 是隐藏的还是公开的。
This is the pseudocode:
这是伪代码:
if(DIV != VISIBLE) // not visible { show content }
Any JQuery Experts able to assist me?
任何 JQuery 专家能够帮助我?
Thanks, Robert
谢谢,罗伯特
回答by nickf
If you're trying to justshow a div which is hidden, then you don't actually need to do any checks at all:
如果您只想显示一个隐藏的 div,那么您实际上根本不需要进行任何检查:
$('#myDiv').show();
Regardless of its state beforehand, it'll end up visible now.
不管它之前的状态如何,它现在最终都会可见。
However, if you want to perform other actions depending on whether the content is visible or not, then you'll need to check:
但是,如果您想根据内容是否可见来执行其他操作,则需要检查:
if ($('#myDiv').is(":hidden"))
// or
if ($('#myDiv:hidden').length)
// or
if ($('#myDiv:not(:visible)')) { // you get the idea...
//perform your actions
}
回答by eKek0
All other answers are fine, but this is just to translate your pseudocode into actual javascript code:
所有其他答案都很好,但这只是将您的伪代码转换为实际的 javascript 代码:
if (!$('div').is(':visible')) {
$('div').show();
}
回答by tvanfosson
The following will display a DIV named myDiv
if it is hidden. Note that if you want to do other stuff, you can also use each() rather than show and do other operations on $(this) inside each.
myDiv
如果隐藏,下面将显示一个名为 DIV 的 DIV 。请注意,如果您想做其他事情,也可以使用 each() 而不是 show 并在 each 内部对 $(this) 执行其他操作。
$('div#myDiv:hidden').show();
回答by womp
This will test to see if you selected any hidden elements with the id "mydiv":
这将测试您是否选择了 ID 为“mydiv”的任何隐藏元素:
if ( $("#mydiv:hidden").length > 0)
{
//
}
Edit: Simplified selector. Had to look it up :/
编辑:简化的选择器。不得不查一下:/
回答by user2128205
If you're only looking to hiding and showing an element you might as well just use jQuery .toggle()method that will do just that for you. Similarly, .slideToggle()and .fadeToggle() methods will hide and show element(s) with either slide or fade animation.
如果您只想隐藏和显示一个元素,您不妨使用 jQuery .toggle()方法,它可以为您做到这一点。类似地,.slideToggle()和 .fadeToggle() 方法将隐藏和显示带有幻灯片或淡入淡出动画的元素。