jQuery 如何检查div是否处于可见状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12353741/
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
How to check if a div is visible state or not?
提问by Aristona
I have tabs like this.
我有这样的标签。
<li id="singlechatpanel-1" style="visibility: hidden;">
//content
</li>
Trying to check it like this:
试图像这样检查它:
$(".subpanel a").click(function()
{
var chatterNickname = $(this).text();
if(!$("#singlechatpanel-1").is(':visible'))
{
alert("Room 1 is filled.");
$("#singlechatpanel-1").css({'visibility':'visible'});
$("#singlechatpanel-1 #chatter_nickname").html("Chatting with: " + chatterNickname);
}
If condition always returns false. How can I check visibility state of this div?
如果条件总是返回假。如何检查此 div 的可见性状态?
回答by undefined
is(':visible')
checks the display
property of an element, you can use css
method.
is(':visible')
检查display
元素的属性,您可以使用css
方法。
if (!$("#singlechatpanel-1").css('visibility') === 'hidden') {
// ...
}
If you set the display
property of the element to none
then your if
statement returns true
.
如果您将display
元素的属性设置为,none
那么您的if
语句将返回true
。
回答by Vipin Kumar R. Jaiswar
Check if it's visible.
检查它是否可见。
$("#singlechatpanel-1").is(':visible');
$("#singlechatpanel-1").is(':visible');
Check if it's hidden.
检查它是否隐藏。
$("#singlechatpanel-1").is(':hidden');
$("#singlechatpanel-1").is(':hidden');
回答by Shah Nawaz
if element is hide by jquery then use
如果元素被 jquery 隐藏,则使用
if($("#elmentid").is(':hidden'))
回答by Pablo Martinez
回答by Prabhagaran
if (!$('#singlechatpanel-1').css('display') == 'none') {
alert('visible');
}else{
alert('hidden');
}
回答by Sushanth --
You can use (':hidden') method to find if your div is visible or not.. Also its a good practice to cache a element if you are using it multiple times in your code..
您可以使用 (':hidden') 方法来查找您的 div 是否可见。如果您在代码中多次使用它,那么缓存一个元素也是一个好习惯。
$(".subpanel a").click(function()
{
var chatterNickname = $(this).text();
var $chatPanel = $("#singlechatpanel-1");
if(!$chatPanel.is(':hidden'))
{
alert("Room 1 is filled.");
$chatPanel.show();
$("#singlechatpanel-1 #chatter_nickname").html("Chatting with: " + chatterNickname);
}
});
回答by PhonicUK
Add your li
to a class, and do $(".myclass").hide();
at the start to hide it instead of the visibility style attribute.
将你的添加li
到一个类中,并$(".myclass").hide();
在开始时隐藏它而不是可见性样式属性。
As far as I know, jquery uses the display
style attribute to show/hide elements instead of visibility (may be wrong on that one, in either case the above is worth trying)
据我所知,jquery 使用display
style 属性来显示/隐藏元素而不是可见性(可能是错误的,无论哪种情况,以上都值得一试)