Disabling onclick attribute with javascript

前端 未结 7 618
梦如初夏
梦如初夏 2021-01-05 14:09

How do I disable the other onclick event once one of them has been activated. Both divs are set to display:none; in the CSS. This is probably really simple but I am new to p

相关标签:
7条回答
  • 2021-01-05 15:07

    You can set the href attribute as id of div which should be visibled after you click. showDiv function can get entire element as argument, then you have access to it attributes as well. Second argument can be a id of element you should hide,

       <a id="leftbutton" href="orangediv" onclick="showDiv(this,'rightbutton');return false">click me</a>
       <a id="righttbutton" href="greendiv"onclick="showDiv(this,'leftbutton');return false">click me</a>
    

    And in js:

    var showDiv =function(el, toHide){
      document.getElementById( el.href ).style.display = 'block';
      document.getElementById( toHide ).style.display = 'none';
      el.onclick = null; //remove onclick declaration from this anchor.
    
    }
    
    0 讨论(0)
提交回复
热议问题