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

前端 未结 8 2089
青春惊慌失措
青春惊慌失措 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: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

提交回复
热议问题