如何在 JavaScript 中使用 x,y 坐标模拟点击?
是否可以使用给定的坐标来模拟网页中 JavaScript 的点击?
Is it possible to use given coordinates in order to simulate a click in JavaScript within a webpage?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以调度点击事件,尽管这与真正的点击不同。例如,它不能用于欺骗跨域 iframe 文档,使其认为它已被单击。
所有现代浏览器都支持
document.elementFromPoint
和HTMLElement.prototype.click()
,因为至少 IE 6、Firefox 5、任何版本的 Chrome 以及可能任何版本的 Safari可能会关心。它甚至会跟踪链接并提交表单:You can dispatch a click event, though this is not the same as a real click. For instance, it can't be used to trick a cross-domain iframe document into thinking it was clicked.
All modern browsers support
document.elementFromPoint
andHTMLElement.prototype.click()
, since at least IE 6, Firefox 5, any version of Chrome and probably any version of Safari you're likely to care about. It will even follow links and submit forms:是的,您可以通过创建事件并分派它来模拟鼠标单击:
请注意在元素上使用
click
方法 - 它已被广泛实现,但不是标准的,并且在 PhantomJS 等中会失败。我认为 jQuery 的.click()
实现做了正确的事情,但尚未证实。Yes, you can simulate a mouse click by creating an event and dispatching it:
Beware of using the
click
method on an element -- it is widely implemented but not standard and will fail in e.g. PhantomJS. I assume jQuery's implemention of.click()
does the right thing but have not confirmed.这只是 torazaburo 的答案,已更新为使用 MouseEvent 对象。
This is just torazaburo's answer, updated to use a MouseEvent object.
它对我有用,但它会将正确的元素打印到控制台,
这是代码:
it doenst work for me but it prints the correct element to the console
this is the code:
出于安全原因,您不能使用 JavaScript 移动鼠标指针,也不能用它模拟单击。
你想要实现什么目标?
For security reasons, you can't move the mouse pointer with javascript, nor simulate a click with it.
What is it that you are trying to accomplish?