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

两盒软妹~` 提交于 2019-11-29 04:36:53

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);
e = e || window.event;
keycode = e.keyCode || e.which;

That will make events work in all browsers. Also, I prefer to use me.onkeyup = function(e) { ... }, but that' just personal preference (I know the drawbacks of this method, but I have clever ways to work around them)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!