Why keydown listener doesn't work in IE

后端 未结 2 1290
隐瞒了意图╮
隐瞒了意图╮ 2020-12-11 18:37

Im trying to run this in IE 8 but it doesn\'t work, any idea? It works in Firefox, Chrome, Opera...

preventBackspace();

function preventBackspace() {
    tr         


        
相关标签:
2条回答
  • 2020-12-11 18:51

    Use document.attachEvent instead. :]

    0 讨论(0)
  • 2020-12-11 19:12

    It appears that only IE9 and later support binding keydown on window: http://www.quirksmode.org/dom/events/keys.html#t00

    Instead, bind it to the document for IE:

    function preventBackspace() {
        try {
            if (window.addEventListener) {
                window.addEventListener("keydown", onKeyDown, true);
            } else if (document.attachEvent) { // IE 
                alert(document);
                document.attachEvent("onkeydown", onKeyDown);
            } else {
                document.addEventListener("keydown", onKeyDown, true);
            }
        } catch (e) {
            alert(e);
        }
    }
    
    0 讨论(0)
提交回复
热议问题