javascript模拟鼠标点击特定位置

标签 javascript click mouse position simulate

我现在需要了解如何在按钮上自动触发鼠标单击事件。 我有这个但是不起作用:(

window.setInterval(function() { simulateClick(); }, 2000);

function simulateClick() {
    var evt = document.createEvent("MouseEvents");
    evt.initMouseEvent("click", false, false, window, 0, 684, 571, 684, 508, false, false, false, false, 0, null);
        var a;
    a.dispatchEvent(evt);
}

提前致谢

奥斯卡

最佳答案

如果你只想点击一个按钮,按钮元素有一个可以调用的click方法:

<input type="button" id="theButton" onclick="javascript:alert('The button was clicked!');" value="A button" />

<script language="javascript">

setTimeout(function(){
    document.getElementById("theButton").click();
}, 1000); // wait one second then click the button

</script>

无需“实际”模拟鼠标在特定 x,y 位置的点击。

关于javascript模拟鼠标点击特定位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5916839/

相关文章:

Ubuntu 9.10上的Eclipse Galileo单击问题

javascript - 如何导出node.js中请求生成的变量

javascript - 正则表达式每个点换行,但仅在引号之外

php - ajax 不适用于 LIKE 按钮

javascript - 如何触发TouchableOpacity的onPress事件?

python - Tkinter - 当前鼠标指针在哪个小部件上?

javascript - 如何将跨度注入(inject)字符串并将其作为 HTML 打印在我的 React 应用程序中

javascript - jQuery: .toggle code atlernative 在第二次点击时不起作用

.net - 鼠标移动时如何检测shift键按下状态

c# - 什么会导致 PostMessage 发送的鼠标点击被忽略?