javascript 自動觸發 按鍵盤操作 trigger


回車鍵 keyCode 13

HTMLElement.prototype.pressKey = function(code) {
var evt = document.createEvent("UIEvents");
evt.keyCode = code;
evt.initEvent("keydown", true, true);
this.dispatchEvent(evt);
}

  

test:

document.body.onkeydown = function(e) {
    console.log("key pressed, code=" + e.keyCode);
};

document.body.pressKey(13);

  


 eventType 的合法值和每個值創建的事件接口:

參數 事件接口 初始化方法
HTMLEvents HTMLEvent iniEvent()
MouseEvents MouseEvent iniMouseEvent()
UIEvents UIEvent iniUIEvent()
參照文檔:

http://www.w3school.com.cn/xmldom/met_document_createevent.asp

http://www.w3school.com.cn/xmldom/dom_event.asp
————————————————
版權聲明:本文為CSDN博主「fareast_mzh」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/fareast_mzh/article/details/83416064


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM