How can I generate a keyup event with a specific keycode in IE8?

前端 未结 2 2145
眼角桃花
眼角桃花 2020-12-17 02:00

I need to generate keyup events in IE 8 using native DOM functions (no jQuery). The following code generates, fires, and receives the event, but the keyCode is always 0. How

2条回答
  •  误落风尘
    2020-12-17 02:37

    Figured it out. The solution is to create an event object, assign the keycode, and fire it from the node.

    var e = document.createEventObject("KeyboardEvent");
    e.keyCode = keyCode;
    
    node.fireEvent("onkeyup", e);
    

提交回复
热议问题