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

前端 未结 6 506
故里飘歌
故里飘歌 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:18

    you might wanna try this..

        function switchVisible() {
        	var div1=document.getElementById('Div1');
        	var div2=document.getElementById('Div2');
        	
          if (div1 !== undefined && div2 !== undefined) {
        	  div1.style.display = div2.style.display === '' ? 'none' : div2.style.display === 'none' ? 'none' : 'block';
        	  div2.style.display = div1.style.display === 'block' ? 'none' : 'block';
          }
        }
    #Div1{
     display: block;
     background: blue;
    }
    
    #Div2 {
      display: none;
      background: red;
    }
    
    .divs
    {
     width: 50px;
     height: 50px;
     border: 1px solid #000;
    }
    <input id="Button1" type="button" value="Hide" onclick="switchVisible();" />
    
    <div id="Div1" class="divs"> </div>
    <div id="Div2" class="divs"> </div>

    0 讨论(0)
  • 2020-12-06 03:19

    Don't add the click event through html. Add an event listener like document.getElementById ("Button1").addEventListener ("click", switchVisible, false);

    See working jsFiddle here: http://jsfiddle.net/bevanr01/gmkconLw/

    0 讨论(0)
  • 2020-12-06 03:21

    Try this:

    var div1 = document.getElementById('Div1'),
        div2 = document.getElementById('Div2');
    function switchVisible() {
      if(!div1) return;
      if (getComputedStyle(div1).display == 'block') {
        div1.style.display = 'none';
        div2.style.display = 'block';
      } else {
        div1.style.display = 'block';
        div2.style.display = 'none';
      }
    }
    document.getElementById('Button1').addEventListener('click', switchVisible);
    #Div2 {
      display:none;
    }
    <div id="Div1">Div 1</div>
    <div id="Div2">Div 2</div>
    <input id="Button1" type="button" value="Click me" />


    However, this approach may be better:

    var wrapper = document.getElementById('wrapper');
    function switchVisible() {
      wrapper.classList.toggle('switched');
    }
    document.getElementById('Button1').addEventListener('click', switchVisible);
    #wrapper > :last-child {
      display: none;
    }
    #wrapper.switched > :last-child {
      display: block;
    }
    #wrapper.switched > :first-child {
      display: none;
    }
    <div id="wrapper">
      <div>Div 1</div>
      <div>Div 2</div>
    </div>
    <input id="Button1" type="button" value="Click me" />

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-06 03:30

    Probably you have some syntax errors in your code. This one is working:

    function switchVisible() {
        if (document.getElementById('Div1') !== undefined) {
    
            if (document.getElementById('Div1').style.display == 'block') {
                document.getElementById('Div1').style.display = 'none';
                document.getElementById('Div2').style.display = 'block';
            } else {
                document.getElementById('Div1').style.display = 'block';
                document.getElementById('Div2').style.display = 'none';
            }
        }
    }
    #Div1 {
        display:none;
    }
    #Div1 {
        background: red;
    }
    #Div2 {
        background: green;
    }
    #Div1, #Div2 {
        width: 50px;
        height:50px;
    }
    <div id="Div1"></div>
    <div id="Div2"></div>
    <input id="Button1" type="button" value="" onclick="javascript:switchVisible();" />

    0 讨论(0)
  • 2020-12-06 03:32

    1) Inside onclick, you don't have to use "javascript:", that is implied.

    2) You check for "display: block", I always check for "display: none" (Because the display can also be "inline-block", etc.)

    Try this:

    function switchVisible() {
                if (document.getElementById('Div1')) {
    
                    if (document.getElementById('Div1').style.display == 'none') {
                        document.getElementById('Div1').style.display = 'block';
                        document.getElementById('Div2').style.display = 'none';
                    }
                    else {
                        document.getElementById('Div1').style.display = 'none';
                        document.getElementById('Div2').style.display = 'block';
                    }
                }
    }
    #Div2 {
      display: none;
    }
    <div id="Div1">Div 1</div>
    <div id="Div2">Div 2</div>
    
    <input id="Button1" type="button" value="Click" onclick="switchVisible();"/>

    0 讨论(0)
提交回复
热议问题