如何用JavaScript模拟单击?我只是想知道如何使用JavaScript来模拟对元素的单击。目前我有:<script type="text/javascript">function simulateClick(control){
if (document.all)
{
control.click();
}
else
{
var evObj = document.createEvent('MouseEvents');
evObj.initMouseEvent('click', true, true, window, 1, 12, 345, 7, 220, false, false, true, false, 0, null );
control.dispatchEvent(evObj);
}}</script><a href="http://www.google.com" id="mytest1">test 1</a><br><script type="text/javascript">
simulateClick(document.getElementById('mytest1'));</script>但这不起作用有什么想法吗?
3 回答
慕莱坞森
TA贡献1810条经验 获得超4个赞
function eventFire(el, etype){ if (el.fireEvent) { el.fireEvent('on' + etype); } else { var evObj = document.createEvent('Events'); evObj.initEvent(etype, true, false); el.dispatchEvent(evObj); }}
eventFire(document.getElementById('mytest1'), 'click');
添加回答
举报
0/150
提交
取消