如何使用 JavaScript 中的 x,y 坐标模拟点击? [英] How to simulate a click by using x,y coordinates in JavaScript?
问题描述
是否可以使用给定的坐标来模拟网页中 JavaScript 中的点击?
Is it possible to use given coordinates in order to simulate a click in JavaScript within a webpage?
推荐答案
您可以调度 click 事件,尽管这与真正的点击不同.例如,它不能用于诱使跨域 iframe 文档认为它已被点击.
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.
所有现代浏览器都支持 document.elementFromPoint
和 HTMLElement.prototype.click()
,因为至少 IE 6、Firefox 5、任何版本的 Chrome 和可能的任何版本您可能会关心的 Safari 浏览器.它甚至会跟踪链接并提交表单:
All modern browsers support document.elementFromPoint
and HTMLElement.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:
document.elementFromPoint(x, y).click();
- https://developer.mozilla.org/en-US/docs/Web/API/Document/elementFromPoint
- https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click
这篇关于如何使用 JavaScript 中的 x,y 坐标模拟点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!