Disabling onclick attribute with javascript

2019-02-10 14:10发布

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 programming.

HTML

<a id="leftbutton" href="#" onclick="showDiv(this.id); return false;">click me</a>
<a id="righttbutton" href="#"onclick="showDiv(this.id); return false;">click me</a>

Javascript

function showDiv(id)
{   
  if(id == "leftbutton"){
     document.getElementById('orangediv').style.display = 'block';
     }else{
     document.getElementById('greendiv').style.display = 'block';
}}

7条回答
SAY GOODBYE
2楼-- · 2019-02-10 15:11
function showDiv(id) {
    if ( showDiv.executed ) {
        return false;
    }

    if  ( id === "leftbutton" ) {
       document.getElementById('orangediv').style.display = 'block';
    } else {
       document.getElementById('greendiv').style.display = 'block';
    }

    showDiv.executed = true;
}

showDiv.executed = false;

Doing it this way, you could always re-enable the showing by simply setting showDiv.executed to false.

查看更多
登录 后发表回答