在特定坐标处触发 JavaScript click() 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2845178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Triggering a JavaScript click() event at specific coordinates
提问by OneNerd
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.
试图触发(触发)点击事件。在 jQuery 中很容易做到,但无法弄清楚如何设置事件的坐标并将它们一起发送。
Essentially, I need to trigger a click at a specific location (which is calculated prior to the trigger() call).
本质上,我需要在特定位置触发点击(这是在 trigger() 调用之前计算的)。
Any way to do this (in jQuery or otherwise)?
有什么方法可以做到这一点(在 jQuery 或其他方式中)?
Thanks -
谢谢 -
回答by Nick Craver
Set the pageXand pageYproperties (which are normalized) on the event objectand pass it to .trigger(), like this:
在事件对象上设置pageX和pageY属性(已规范化)并将其传递给,如下所示:.trigger()
var e = new jQuery.Event("click");
e.pageX = 10;
e.pageY = 10;
$("#elem").trigger(e);
回答by BradBrening
The ".trigger()" call accepts an "extraParameters" parameter that you could use to stuff the XY coords in I would think. Documentation
“.trigger()”调用接受一个“extraParameters”参数,你可以用它来填充我认为的 XY 坐标。 文档
.trigger("click", [x, y]);

