简体   繁体   中英

How to show an hidden element by clicking on another element

Here's a fiddle :
http://fiddle.jshell.net/ggw6hqsj/1/

Here's my problem (for example) :
When the first button is clicked, the second is hidden. But I don't know how to make reappear the hidden button when the first button is clicked once again.

Any idea ? Thanks.

$(document).ready(function(){ $('.button1').click(function(){ $('.button2').toggle(); }); });

http://fiddle.jshell.net/ggw6hqsj/3/

The easiest way in your code would be to change

$('.button2').hide();

to

$('.button2').toggle();

for button 1 and 2.

fiddle

现有代码最简单的更改可能就是使用.toggle()而不是.toggle() .hide()

$('.button2').toggle();

You only need to show the clicked button and hide the other one.

try this

function activateButton(num){
    var activeButton = ".button" + num;
    var otherButton = ".button" + (num === "1" ? "2" : "1");
    $(this).toggleClass('active');
    $(activeButton).show();
    $(otherButton).hide();      
}

$(document).ready(function(){
    $('.button1').click(activateButton("1"));
    $('.button2').click(activateButton("2"));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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