Multiple JS event handlers on single element

后端 未结 3 607
刺人心
刺人心 2020-12-02 10:59

I am working with an existing web app, in the app there are a variety of submit buttons on different forms, some using regular http post, some defining an onClick function,

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 11:26

    I am/was having a similar issue as this. However I can not affect the order of/delegate the pre-existing 'click' events (added by Wicket framework). But I still need to execute a new custom event before any of the 'click' or 'change' events handled by the framework.

    Luckily there are several events that are actually executed in order. The 'mousedown' and the 'mouseup' happens to happen before the 'click'.
    http://en.wikipedia.org/wiki/DOM_events

    $(document).on('mousedown', function (event) {
      event = event || window.event
      var target = event.target || event.srcElement;
      console.log(target + ' before default event'); // Hold mouse button down to see this message in console before any action is executed
    });
    

    OR

    $(document).on('mouseup', function (event) {
      event = event || window.event
      var target = event.target || event.srcElement;
      alert(target + ' before default event'); // You may not notice this event fires when the page changes unless this is an alert
    });
    

    This will allow the logging to be done (e.g. via ajax) before the actual event is executed e.g. a page change via (ajax) link.

    Of course you may need to have more sophisticated means to detect for what the additional event handling should be done, but you can use for example the 'target' information for this. => This script monitors everything on the page, as this is how I need this to be done.

提交回复
热议问题