Show/hide 'div' using JavaScript

前端 未结 14 749
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 04:18

For a website I\'m doing, I want to load one div, and hide another, then have two buttons that will toggle views between the div using JavaScript.

This is my

相关标签:
14条回答
  • 2020-11-22 04:32

    You can Hide/Show Div using Js function. sample below

    <script>
        function showDivAttid(){
    
            if(Your Condition) {
    
                document.getElementById("attid").style.display = 'inline';
            }
            else
            {
                document.getElementById("attid").style.display = 'none';
            }
        }
    
    </script>
    

    HTML -

    <div  id="attid" style="display:none;">Show/Hide this text</div>
    
    0 讨论(0)
  • 2020-11-22 04:34

    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
       }
    
    0 讨论(0)
  • 2020-11-22 04:35

    Just Simple Function Need To implement Show/hide 'div' using JavaScript

    <a id="morelink" class="link-more" style="font-weight: bold; display: block;" onclick="this.style.display='none'; document.getElementById('states').style.display='block'; return false;">READ MORE</a>
    
    
    <div id="states" style="display: block; line-height: 1.6em;">
     text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here  
        <a class="link-less" style="font-weight: bold;" onclick="document.getElementById('morelink').style.display='inline-block'; document.getElementById('states').style.display='none'; return false;">LESS INFORMATION</a>
        </div>
    
    0 讨论(0)
  • 2020-11-22 04:36

    You can also use the jQuery JavaScript framework:

    To Hide Div Block

    $(".divIDClass").hide();
    

    To show Div Block

    $(".divIDClass").show();
    
    0 讨论(0)
  • 2020-11-22 04:38

    I found this plain JavaScript code very handy!

    #<script type="text/javascript">
        function toggle_visibility(id) 
        {
            var e = document.getElementById(id);
            if ( e.style.display == 'block' )
                e.style.display = 'none';
            else
                e.style.display = 'block';
        }
    </script>
    
    0 讨论(0)
  • 2020-11-22 04:38

    Just Simple Set the style attribute of ID:

    To Show the hidden div

    <div id="xyz" style="display:none">
         ...............
    </div>
    //In JavaScript
    
    document.getElementById('xyz').style.display ='block';  // to hide
    

    To hide the shown div

    <div id="xyz">
         ...............
    </div>
    //In JavaScript
    
    document.getElementById('xyz').style.display ='none';  // to display
    
    0 讨论(0)
提交回复
热议问题