How to hide/show div tags using JavaScript?

前端 未结 7 888
北海茫月
北海茫月 2020-12-17 20:44

Basically, I\'m trying to make a link that, when pressed, will hide the current body div tag and show another one in its place, unfortunately, when I click the link, the fir

相关标签:
7条回答
  • 2020-12-17 21:04

    Use the following code:

    function hide {
    document.getElementById('div').style.display = "none";
    }
    function show {
    document.getElementById('div').style.display = "block";
    }
    
    0 讨论(0)
  • 2020-12-17 21:05

    just use a jquery event listner , click event. let the class of the link is lb... i am considering body as a div as you said...

    $('.lb').click(function() {
        $('#body1').show();
        $('#body').hide();
     });
    
    0 讨论(0)
  • 2020-12-17 21:07

    Set your HTML as

     <div id="body" hidden="">
     <h1>Numbers</h1>
     </div>
     <div id="body1" hidden="hidden">
     Body 1
     </div>
    

    And now set the javascript as

         function changeDiv()
          {
          document.getElementById('body').hidden = "hidden"; // hide body div tag
          document.getElementById('body1').hidden = ""; // show body1 div tag
          document.getElementById('body1').innerHTML = "If you can see this, JavaScript function worked"; 
          // display text if JavaScript worked
           }
    

    Check, it works.

    0 讨论(0)
  • 2020-12-17 21:17

    Consider using jQuery. Life is much easier with:

    $('body').hide(); $('body1').show();
    
    0 讨论(0)
  • 2020-12-17 21:19

    Have you tried

    document.getElementById('body').style.display = "none";

    instead of

    document.getElementById('body').style.display = "hidden";?

    0 讨论(0)
  • 2020-12-17 21:23

    try yo write document.getElementById('id').style.visibility="hidden";

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