Domanda

Sto cercando di eseguire questo in IE 8 ma non funziona, nessuna idea?Funziona in Firefox, Chrome, Opera ...

preventBackspace();

function preventBackspace() {
    try {
        if (window.addEventListener) {
            window.addEventListener("keydown", onKeyDown, true);
        } else if (window.attachEvent) { // IE 
            alert(window);
            window.attachEvent("onkeydown", onKeyDown);
        } else {
            document.addEventListener("keydown", onKeyDown, true);
        }
        } catch (e) {
            alert(e);
    }
}

function onKeyDown(e) {
    alert("test!");
}
.

jsfiddle:

http://jsfiddle.net/ubfbq/

Window.attachevent è definito e ha aggiunto l'ascoltatore degli eventi.Ma non mostra mai "test!"Alert.

Leggo qualcosa sulla bandiera UseCapture, che è possibile utilizzare negli altri metodi.Cattura la chiave della chiave sulla finestra prima che l'evento "va giù".Internet Explorer non sembra consentire / usare questo.È il problema?Se sì, come posso risolverlo?

È stato utile?

Soluzione

Use document.attachEvent instead. :]

Altri suggerimenti

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);
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top