Frage

I have some code like this:

$(document).on("keydown", ":not(input)", function(event) {
    if (event.which === 8) {
        event.preventDefault();
    }
});

Everything is working fine except the :not(input) selector. I can't backspace in any input box.

What am I doing wrong?

War es hilfreich?

Lösung

I can't explain exactly why it's happening this way, it's likely due to the way the event propagates up the dom, however, removing the delegation and using an event.target check seems to work.

http://jsfiddle.net/7KDMn/3/

$(document).on("keydown", function(event) {
    if ( event.which === 8 && !$(event.target).is("input") ) {
        event.preventDefault();
    }
});
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top