javascript-实现鼠标事件模拟

鼠标事件模拟

  • 鼠标事件模拟
    • 函数库
    • 点击事件

函数库

  1. EventUtil.js
  2. BrowserUtil.js

点击事件

<button id="btn1">按钮1button> p> <p> <button id="btn2">按钮2button> 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) { // IE8鼠标事件模拟 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 { // IE9及以上和常用浏览器的鼠标事件模拟 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>

你可能感兴趣的:(javascript)