[removed] simulate a click on a link

前端 未结 7 1967
耶瑟儿~
耶瑟儿~ 2020-12-20 21:39

I have a link that has a listener attached to it (I\'m using YUI):

YAHOO.util.Event.on(Element, \'click\', function(){ /* some functionality */});

相关标签:
7条回答
  • 2020-12-20 22:30

    MDC has a good example of using dispatchEvent to simulate click events.

    Here is some code to simulate a click on an element that also checks if something canceled the event:

    function simulateClick(elm) {
      var evt = document.createEvent("MouseEvents");
      evt.initMouseEvent("click", true, true, window,
        0, 0, 0, 0, 0, false, false, false, false, 0, null);
      var canceled = !elm.dispatchEvent(evt);
      if(canceled) {
        // A handler called preventDefault
        // uh-oh, did some XSS hack your site?
      } else {
        // None of the handlers called preventDefault
        // do stuff
      }
    }
    
    0 讨论(0)
提交回复
热议问题