How to disable the onclick event once it happens?

前端 未结 4 993
失恋的感觉
失恋的感觉 2020-12-16 20:55

I had a image which calls add_cart() JavaScript function when onclick()


<         


        
相关标签:
4条回答
  • 2020-12-16 21:25

    Using

    <img src="images/add.png" onclick="this.onclick = function(){};add_cart();">
    
    0 讨论(0)
  • 2020-12-16 21:25
    window.onload = function() {
        document.getElementById('myImageId').onclick = handleImageClick;
    }
    
    var handleImageClick = function(e) {
        var target;
        if (!e) var e = window.event;
        if (e.target) targ = e.target;
        else if (e.srcElement) targ = e.srcElement;
        if (targ.nodeType == 3) // defeat Safari bug
            targ = targ.parentNode;
    
        if(target) {
            target.onclick = function(){}
    
            // do your stuff here
        }
    }
    
    0 讨论(0)
  • 2020-12-16 21:31
    <img src="images/add.png" onclick="add_cart(); this.onclick=null;">
    
    0 讨论(0)
  • 2020-12-16 21:35

    If you are dealing this on jquery side,you can use jquery one method

    $(".myDiv").one("click", function(){  
        alert("this will happen only once");  
    });
    

    Cheers

    0 讨论(0)
提交回复
热议问题