在纯JavaScript中模拟KeyPress Enter for Google Chrome

时间:2016-03-14 23:22:58

标签: javascript html dom

我正在尝试模拟Enter键。我尝试这样做似乎没有发生任何事情:

https://jsfiddle.net/jn4zy1fc/4/

  <form>
    <input type="text" id="field" onkeydown="test(event);">
  </form>

  <script>
     function test(ev){
        if(ev.keyCode == 13){
        alert ("enter key");
        }
     }

        field = document.getElementById("field");
        var e = document.createEvent("KeyboardEvent"); 
        e.initKeyboardEvent("keydown", true, true, window,true, false, false, false,13, 0);
        field.dispatchEvent(e);     
  </script>

0 个答案:

没有答案