how to toggle (hide/show) a table onClick of tag in java script

前端 未结 8 2109
青春惊慌失措
青春惊慌失措 2021-02-05 13:24

I want to show and hide (toggle) the

onClick event of the . this is my tag

&         


        
                      
相关标签:
8条回答
  • 2021-02-05 13:28

    You are always passing in true to the toggleMethod, so it will always "show" the table. I would create a global variable that you can flip inside the toggle method instead.

    Alternatively you can check the visibility state of the table instead of an explicit variable

    0 讨论(0)
  • 2021-02-05 13:32

    visibility property makes the element visible or invisible.

    function showTable(){
        document.getElementById('table').style.visibility = "visible";
    }
    function hideTable(){
        document.getElementById('table').style.visibility = "hidden";
    }
    
    0 讨论(0)
  • 2021-02-05 13:37

    You are trying to alter the behaviour of onclick inside the same function call. Try it like this:

    Anchor tag

    <a id="loginLink" onclick="toggleTable();" href="#">Login</a>
    

    JavaScript

    function toggleTable() {
        var lTable = document.getElementById("loginTable");
        lTable.style.display = (lTable.style.display == "table") ? "none" : "table";
    }
    
    0 讨论(0)
  • 2021-02-05 13:39

    inside your function toggleTable when you do this line

    document.getElementById("loginLink").onclick = toggleTable(....
    

    you are actually calling the function again. so toggleTable gets called again, and again and again, you're falling in an infinite recursive call.

    make it simple.

    function toggleTable()
    {
         var elem=document.getElementById("loginTable");
         var hide = elem.style.display =="none";
         if (hide) {
             elem.style.display="table";
        } 
        else {
           elem.style.display="none";
        }
    }
    

    see this fiddle

    0 讨论(0)
  • 2021-02-05 13:41

    Your anchor tag should be:

      <a id="loginLink" onclick="showHideTable();" href="#">Login</a>
    

    And You javascript function :

    function showHideTable()
    {
       if (document.getElementById("loginTable").style.display == "none" ) {
           document.getElementById("loginTable").style.display="";
    
       } else {
          document.getElementById("loginTable").style.display="none";
    
    }
    
    0 讨论(0)
  • 2021-02-05 13:45

    You need to modify your function as:

    function toggleTable()
    {
       if (document.getElementById("loginTable").style.display == "table" ) {
           document.getElementById("loginTable").style.display="none";
    
       } else {
          document.getElementById("loginTable").style.display="table";
    
    }
    

    currently it is checking based on the boolean parameter, you don't have to pass the parameter with your function.

    You need to modify your anchor tag as:

    <a id="loginLink" onclick="toggleTable();" href="#">Login</a>
    
    0 讨论(0)
提交回复
热议问题