Cancel jQuery event handling

后端 未结 4 1700
萌比男神i
萌比男神i 2021-02-14 12:36

I have setup onclick event handler in the following manner:

element.onclick = function() { /*code */ }

Imagine there are event handlers setup u

4条回答
  •  旧时难觅i
    2021-02-14 13:21

    Following on from JimmyP's answer. I've tried this

    $('#x').click( function(e){
        alert('hello');
    });
    document.getElementById('x').onclick = function(){
        $('#x').unbind('click');
        alert("goodbye");
    }
    

    The jQuery event runs once in this example. I don't think you can rely on the order of handlers being invoked however you define them, so I guess you'll have to accept that the jQuery event might fire once. Adding the onclick first does prevent the jQuery event from firing at all but, as I said, I don't think that's reliable.

提交回复
热议问题