Is it possible to remove all event handlers of a given element in javascript?

前端 未结 2 533
失恋的感觉
失恋的感觉 2020-12-15 15:08

I would like to remove ALL handlers for a given event type. Let\'s say I\'ve added twice \"onclick event\" to a button and now I would like to return back to the original st

2条回答
  •  难免孤独
    2020-12-15 15:59

    It might be a good idea to use jQuery or a similar framework to manage all event handlers. This will give you easy-to-use, unobtrusive functions to add and remove event handlers:

    $(...).on('click', function() { ... });
    $(...).off('click');
    // or, to unbind all events:
    $(...).off();
    

提交回复
热议问题