100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > JS模拟鼠标点击按钮

JS模拟鼠标点击按钮

时间:2024-06-19 07:29:54

相关推荐

JS模拟鼠标点击按钮

var btn = document.querySelector("#su");// 先要注册,然后触发,否则不行。btn.addEventListener("click", function(e) {console.log("simulate event");}, false);var event = document.createEvent("MouseEvents");event.initMouseEvent('click',true,true,document.defaultView,0,0,0,0,0,false,false,false,false,0,null);btn.dispatchEvent(event)

//模拟按住Shift的同时又按下A键:var text = document.getElementsByTagNames("input")[0];if (document.implementation.hasFeature("KeyboardEvents", 3.0) {var event = document.createEvent("KeyboardEvent");event.initKeyboardEvent("keydown", true, true, document.defaultView, "a", 0, "Shift", 0);}

function simulatedClick(target, options) {var event = target.ownerDocument.createEvent('MouseEvents'),options = options || {},opts = {// These are the default values, set up for un-modified left clickstype: 'click',canBubble: true,cancelable: true,view: target.ownerDocument.defaultView,detail: 1,screenX: 0, //The coordinates within the entire pagescreenY: 0,clientX: 0, //The coordinates within the viewportclientY: 0,ctrlKey: false,altKey: false,shiftKey: false,metaKey: false, //I *think* 'meta' is 'Cmd/Apple' on Mac, and 'Windows key' on Win. Not sure, though!button: 0, //0 = left, 1 = middle, 2 = rightrelatedTarget: null,};//Merge the options with the defaultsfor (var key in options) {if (options.hasOwnProperty(key)) {opts[key] = options[key];}}//Pass in the optionsevent.initMouseEvent(opts.type,opts.canBubble,opts.cancelable,opts.view,opts.detail,opts.screenX,opts.screenY,opts.clientX,opts.clientY,opts.ctrlKey,opts.altKey,opts.shiftKey,opts.metaKey,opts.button,opts.relatedTarget );//Fire the eventtarget.dispatchEvent(event);}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。