How to hide one div and show another div using button onclick?

前端 未结 6 631
故里飘歌
故里飘歌 2020-12-06 03:06

I have two div in my html file. I want to hide the 1st div and show another div on html input button onclick event.

Here is my code,

6条回答
  •  生来不讨喜
    2020-12-06 03:22

    As it is tagged with jQuery, here's the simple jQuery answer

    $('body').on("click touchstart", "#Button1", function(e){
       $("#Div1, #Div2").toggle();
    });
    

    use on to listen for the id #Button, I've used both click and touchstart to make it mobile friendly, and then used toggle() which sets the state of the display: to the opposite to what it is now. So if it was display:none, it becomes display:block, if it was display:block it becomes display:none

提交回复
热议问题