鼠标事件模拟
函数库
- EventUtil.js
- BrowserUtil.js
点击事件
<p>
<button id="btn1">按钮1</button>
</p>
<p>
<button id="btn2">按钮2</button>
</p>
<script>
(function(){
var btn1 = document.querySelector('#btn1');
var btn2 = document.querySelector('#btn2');
EventUtil.addHandler(btn1, 'click', function (event) {
event = EventUtil.getEvent(event);
alert('btn1');
alert(event.screenY);
});
EventUtil.addHandler(btn2, 'click', function (event) {
var browser = BrowserUtil.browserVersion()
if (browser.name === 'ie' && +browser.version <= 8) {
event = document.createEventObject();
event.screenX = 0;
event.screenY = 100;
event.clientX = 0;
event.clientY = 0;
event.ctrlKey = false;
event.altKey = false;
event.shiftKey = false;
event.button = 0;
btn1.fireEvent('onclick', event);
} else {
event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, true, document.defaultView, 0, 0, 100, 0, 0, false, false, false, false, 0, btn2);
btn1.dispatchEvent(event);
}
})
})()
</script>