Triggering a JavaScript click() event at specific coordinates

痴心易碎 提交于 2019-12-17 02:29:59

问题


Trying to fire off (trigger) a click event. Its easy to do in jQuery, but cannot figure out how to set the coordinates of the event and send them along.

Essentially, I need to trigger a click at a specific location (which is calculated prior to the trigger() call).

Any way to do this (in jQuery or otherwise)?

Thanks -


回答1:


Set the pageX and pageY properties (which are normalized) on the event object and pass it to .trigger(), like this:

var e = new jQuery.Event("click");
e.pageX = 10;
e.pageY = 10;
$("#elem").trigger(e);



回答2:


The ".trigger()" call accepts an "extraParameters" parameter that you could use to stuff the XY coords in I would think. Documentation

.trigger("click", [x, y]);


来源:https://stackoverflow.com/questions/2845178/triggering-a-javascript-click-event-at-specific-coordinates

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