How to set target property when simulating mouseclick in javascript?

試著忘記壹切 提交于 2019-12-01 18:08:53
Martin Ernst

The target property of the event object can not be accessed when the event is initialized. It points to the first element that receives the event once it is triggered and is set internally by JavaScript to that element. So if you need a special element being the event target you have to dispatch the event to that element.

One way would be to overwrite the native target property with your own method, but this may affect the bubbling behavior of the event.

dojo.query(".mybutton").forEach(function (node) {
  var target = dojo.query(".myclass").parents("#" + node.id)[0];
  var event = new MouseEvent('click', {
    'view' : window,
    'bubbles': true,
    'cancelable': true
  });
  Object.defineProperty(event, 'target', {value: target, enumerable: true});
  dijit.byId(node.id)._doEvent(event);
});
Ferry Kranenburg

You will need to create an event first. See here how to simulate a mouse event.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!