模拟鼠标事件

DOM

 1 var btn = document.getElementById("myBtn");

 2  

 3  //创建事件对象

 4  var event = document.createEvent("MouseEvents");

 5  

 6  //初始化事件对象

 7  event.initMouseEvent("click", true, true, document.defaultView, 0, 0, 0, 0, 0, false, false, false, false, 0, null);

 8  

 9  //触发事件

10  btn.dispatchEvent(event);

 

 

IE

 1 var btn = document.getElementById("myBtn");

 2 

 3 //创建事件对象

 4 var event = document.createEventObject();

 5 

 6 //初始化事件对象

 7 event.screenX = 100;

 8 event.screenY = 0;

 9 event.clientX = 0;

10 event.clientY = 0;

11 event.ctrlKey = false;

12 event.altKey = false;

13 event.shiftKey = false;

14 event.button = 0;

15 

16 //触发事件

17 btn.fireEvent("onclick", event);

你可能感兴趣的:(鼠标事件)