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?

有帮助吗?

解决方案

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();
    }
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top