我现在需要了解如何在按钮上自动触发鼠标单击事件。 我有这个但是不起作用:(
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/