简体   繁体   English

jQuery Show&Hide Toggle

[英]jQuery Show & Hide Toggle

I have some divs that need to be hidden (class named .hideable), and some divs that need to be shown (class named .toggleable. I have it working so far, which is great, but I`m having difficulties with the following; The hidden divs (.hideable) need to come back after the toggleable divs are hidden again. 我有一些需要隐藏的div(类名为.hideable),还有一些需要显示的div(类名为.toggleable。我到目前为止工作,这很好,但是我对以下内容有困难) ;隐藏的div(.hideable)需要在可切换的div再次隐藏后返回。

here is what I have: 这就是我所拥有的:

jQuery(document).ready(function($) {
    var topContainer = $(".toggleable");
    var topButton = $(".orsp a");
    topButton.click(function() {
        topContainer.slideToggle('slow');
        $(".hideable").hide();
    });
});

all help is welcome! 欢迎大家帮忙! thanks, J. 谢谢,J。

Use jQuery.toggle() 使用jQuery.toggle()

$(".hideable").toggle();

instead of jQuery.hide() 而不是jQuery.hide()

I think you wnat try like this 我想你wnat尝试喜欢this

HTML HTML

<div style='display:none' class='hideable' >Hidden Div</div>
<div class='toggleable'>Toggleable div</div>

<input class='topButton' type='button' value='toggle'>

JS JS

$('.topButton').click(function() {
    $('.toggleable').slideToggle('slow', function() { $(".hideable").slideToggle(); });
});

Fiddle Here 在这里小提琴

If you don't want to use toggle to hide the .hideable div you can hide it using CSS and whenever you toggle .toggleable div you can check with Jquery whether it is hidden and if it is you can change it back to be shown. 如果您不想使用切换来隐藏.hideable div,您可以使用CSS隐藏它,每当您切换.toggleable div时,您可以使用Jquery检查它是否隐藏,如果是,您可以将其更改为显示。 However Jakub's answer is the simplest solution. 然而,Jakub的答案是最简单的解决方案。

Simplest choose toggle or toggle class 最简单的选择切换或切换类

Fiddle 小提琴

<http://jsfiddle.net/gcsHg/>?
jQuery(document).ready(function($) {
var topContainer = $(".toggleable");
var topButton = $(".orsp a");
topButton.toggle(function() {
    topContainer.slideToggle('slow');
    $(".hideable").hide();
},function () {
topContainer.slideToggle('slow');
    $(".toggleable").hide();
 });
});

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

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