Domanda

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?

È stato utile?

Soluzione

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