Javascript keeps a div hidden until you click a button, need help modifying

前端 未结 2 2030
迷失自我
迷失自我 2021-02-04 07:33

Basically, my code right now keeps a few of the divs I have on my website hidden and then when you click on a link, it makes the divs appear.

I need help so that it so t

相关标签:
2条回答
  • 2021-02-04 08:29

    You could do like this, if you want to display and hide particular div on click of button.

    <style>
    .hidden{
        display:none;
    }
    
    .unhidden{
        display:block;
    }
    </style>
    <script type="text/javascript">
    function unhide(clickedButton, divID) {
    var item = document.getElementById(divID);
    if (item) {
        if(item.className=='hidden'){
            item.className = 'unhidden' ;
            clickedButton.value = 'hide'
        }else{
            item.className = 'hidden';
            clickedButton.value = 'unhide'
        }
    }}
    
    </script>
    
    <body>
    <div id="about" class="hidden">
    <div class="content3">
    <p>This is the about section.</p>
    <p>It is currently still being worked on.</p>
    </div>
    </div>
    <input type="button" onclick="unhide(this, 'about') " value="unhide">
    </body>
    

    UPDATE: Pass the other div id which you want to make disappear on click of div one. Please update your code with below's code.

    SCRIPT

    <script type="text/javascript">
        function unhide(divID, otherDivId) {
        var item = document.getElementById(divID);
        if (item) {
                item.className=(item.className=='hidden')?'unhidden':'hidden';
            }
            document.getElementById(otherDivId).className = 'hidden';
        }
    </script>
    

    HTML

    <p style="padding-top:20px">
        <a href="javascript:unhide('link', 'about')" class="button">This is a link</a><br><br>
        <a href="javascript:unhide('about', 'link')" class="button">About</a>
    </p>
    
    0 讨论(0)
  • 2021-02-04 08:33

    I don't understand your question properly. But if you are trying to make different div elements visible upon clicking different links, then this is what you should do:

    <div id = "anchor-div">
          <ul>
            <li> <a id="about-anchor" href="javascript:;"> About</a> </li> 
            <li> <a id="help-anchor" href="javascript:;"> Help </a> </li>
          </ul>
    </div>
    
    
    <div id="content-div">
           <div id="about-content"></div>
           <div id="help-content"></div>
    </div>
    

    $(document).ready(function(){
    
            //if you wish to keep both the divs hidden by default then dont forget to hide //them           
            $("#help-content").hide();
            $("#about-content").hide();
    
           $("#about-anchor").click(function(){
                 $("#help-content").hide();
                 $("#about-content").show();
            });
    
          $("#help-anchor").click(function(){
                  $("#help-content").show();
                 $("#about-content").hide();
           });
    });
    

    Also don't forget to add jQuery library.

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