admin管理员组

文章数量:1410682

I am using onClick event for the image.

When I am disable for the particular case, I didn't know exactly how can disable the onClick functionality using JavaScript.

Sample code:

<td>
    <a onClick="fun()">
        <html:img src="/caleder.jpg ... />
    </a>
</td>

calling fun:

function fun() {
    // in fun I want to disable the img and onClick functionality
    // i.e when I click that img, then it can't perform the onClick event.
}

I am using onClick event for the image.

When I am disable for the particular case, I didn't know exactly how can disable the onClick functionality using JavaScript.

Sample code:

<td>
    <a onClick="fun()">
        <html:img src="/caleder.jpg ... />
    </a>
</td>

calling fun:

function fun() {
    // in fun I want to disable the img and onClick functionality
    // i.e when I click that img, then it can't perform the onClick event.
}
Share Improve this question edited Oct 23, 2010 at 13:10 Arseni Mourzenko 52.5k35 gold badges118 silver badges210 bronze badges asked Oct 23, 2010 at 13:01 CHANTICHANTI 1,4753 gold badges13 silver badges10 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

The probably most efficient way since it removes the event listener:

<script>
  function fun(node) {
    document.getElementById(node).removeAttribute("onClick");
  }
</script>

Inside of the onClick() you need to place a this so it would be onClick(this)

I usually just set a variable and put the events in the onClick handler inside a conditional statement. Set the variable on the first click, and next time it won't fire.

<script>
  var i = 0;
  function clickHandle(){
    if(i==0){
       //do stuff
       i=1;
  }
<script>

you can easy handle this issue with this code after you function see this codes

function disableClick() {
   document.getElementById('yourId you want disable on click').onclick = "";
}

and enable same this code

The best way IMO.

function fun() 
{
    if (this.clicked == undefined)
    {
        this.clicked = true;
        // have fun
    }
    return false;
}

本文标签: Disable onClick functionality using JavaScriptStack Overflow