Jquery 可见性切换不起作用

发布于 2024-12-21 07:35:08 字数 294 浏览 0 评论 0原文

只是一个简单的问题,有人可以告诉我哪里出了问题吗?我正在尝试切换类的可见性,但无法恢复其可见性。隐藏它很好:

if($(".ball").is(':visible') == true) { 
   $(".ball").css({ 'visibility': 'hidden'}); 
} else { 
    $(".ball").css({ 'visibility': 'visible'}); 
}

我不能使用切换,因为我需要保留该类但不被看到,切换会扰乱格式。

帮助会很好,谢谢。

Just a quick question, can someone tell me where this is going wrong, I'm trying to toggle visibility of a class but I can't restore its visibility. Hiding it is fine:

if($(".ball").is(':visible') == true) { 
   $(".ball").css({ 'visibility': 'hidden'}); 
} else { 
    $(".ball").css({ 'visibility': 'visible'}); 
}

I can't use toggle as I need the class to remain but not be seen, toggle will mess with formatting.

Help would be great, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深爱不及久伴 2024-12-28 07:35:08

选择器“:visible”不能与可见性一起使用,只能与显示一起使用。

这是 jQuery 文档的片段http://api.jquery.com/visible-selector/

具有visibility:hidden或opacity:0的元素被认为是可见的,因为它们仍然占用布局中的空间。在隐藏元素的动画期间,该元素被认为是可见的,直到动画结束。在显示元素的动画期间,该元素被认为在动画开始时可见。

试试这个:

if($(".ball").css("visibility") == "visible") { 
    $(".ball").css({ 'visibility': 'hidden'}); 
} else { 
    $(".ball").css({ 'visibility': 'visible'}); 
}

这是一个示例 http://jsfiddle.net/eGVWM/2/

The selector ":visible" does not work with visibility, only with display

Here is a fragment of the jQuery documentation http://api.jquery.com/visible-selector/:

Elements with visibility: hidden or opacity: 0 are considered to be visible, since they still consume space in the layout. During animations that hide an element, the element is considered to be visible until the end of the animation. During animations to show an element, the element is considered to be visible at the start at the animation.

Try this:

if($(".ball").css("visibility") == "visible") { 
    $(".ball").css({ 'visibility': 'hidden'}); 
} else { 
    $(".ball").css({ 'visibility': 'visible'}); 
}

Here is an example http://jsfiddle.net/eGVWM/2/

呆头 2024-12-28 07:35:08

您可以选择具有 ball 类的所有元素,然后确定其隐藏位置:

$(".ball").each(function() {
    var ball = $(this);
    if (ball.css("visibility") === "hidden")
        ball.css("visibility", "visible");
    else
        ball.css("visibility", "hidden");
});

You can select all elements with the ball class and then determine where or not it is hidden:

$(".ball").each(function() {
    var ball = $(this);
    if (ball.css("visibility") === "hidden")
        ball.css("visibility", "visible");
    else
        ball.css("visibility", "hidden");
});
傲娇萝莉攻 2024-12-28 07:35:08

$(".ball").toggle(); 也会切换,但使用 display:none 进行操作。

$(".ball").toggle(); will toggle too, but behaves using display:none.

淡淡の花香 2024-12-28 07:35:08
if($(".ball").is(':visible') == true) { 
  $(".ball").hide();
 };

可能不是您想要的,但它会隐藏具有 'ball' 类的元素

if($(".ball").is(':visible') == true) { 
  $(".ball").hide();
 };

May not be what you are after but it will hide the elements with 'ball' class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文