Disabling onclick attribute with javascript

蹲街弑〆低调 提交于 2019-11-30 14:19:08

this should do the trick:

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

Like this for example:

function showDiv(id){   

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

Something along these lines:

function showDiv(id){   
var o = document.getElementById('orangediv');
var g = document.getElementById('greendiv');
if  (id == "leftbutton"){
     o.style.display = 'block';
}else{
    g.style.display = 'block';
}
o.writeAttribute('onclick','');
g.writeAttribute('onclick','');
}

Just set the onclick property of the other element to null;

if  (id == "leftbutton"){
        document.getElementById('orangediv').style.display = 'block';
        document.getElementById('righttbutton').onclick = null;
    }
    else{
        document.getElementById('greendiv').style.display = 'block';
        document.getElementById('leftbutton').onclick = null;
    }
}
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.

You could test if the other element is displayed :

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

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.

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!