2011-05-06 55 views
3

我需要現在如何在按鈕上自動點擊鼠標點擊事件。 我有這個,但事先沒有工作:(javascript模擬鼠標點擊特定位置

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); 
} 

感謝

奧斯卡

+0

我的猜測是你沒有啓動一個。 IT只是說「var a;」但你永遠不會指定它是哪個元素。除此之外,我認爲它看起來很好。 – Mamsaac 2011-05-06 20:56:24

+0

如果要模擬測試目的,你可以使用硒http://seleniumhq.org/ – ariel 2011-05-06 21:12:51

+0

貌似你試圖做到這一點: http://stackoverflow.com/questions/6157929/how-模擬鼠標點擊使用javascript/6158050 – Nate 2014-01-07 19:55:50

回答

1

我不知道它更多鈔票來做到這一點的JavaScript但如果你需要自動和定期點擊可能你可以做到這一點與外部工具,如autohotkey

+0

這是可能的。 – Mamsaac 2011-05-06 20:56:53

+0

mmmmm,aybe .....是否可以將指針鼠標自動移動到網頁上的某個位置? – Oscar 2011-05-09 03:00:15

+0

與autohotkey你可以發送一個特定的座標點擊看到這個[頁面](http://www.autohotkey.com/docs/commands/Click.htm) – Dave 2011-05-09 15:55:49

2

如果你想要做的就是點擊一個按鈕,按鈕元素有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位置的點擊。

+2

有IS需要。思考光標位置改變的輸入文本。 – 2016-09-30 09:50:14

+0

@JiangYD就是這樣。題。你可以像這樣在文本框中設置光標的位置:http://stackoverflow.com/questions/512528/set-cursor-position-in-html-textbox – 2016-12-20 12:28:20