开发者

Javascript backspace by default issue....going back a page by default [duplicate]

This question already h开发者_运维技巧as an answer here: Closed 11 years ago.

Possible Duplicate:

javascript backspace by default problem…Going back a page.

The issue I am having is when you type a list of phone numbers into the list and then when you click one to edit it, if you backspace to delete the number, it will mess the entire list up by going back one page....i am fully aware that this is because the default back page is backspace and thats why its doing that but I really want it to not backpage when I use backspace to delete a number from the list.....

So what I am saying basically is how can I disable backspace to back page......

Here is a fiddle:

http://jsfiddle.net/bikbouche1/QVUHU/67/


you can override the Backspace button action using the following code snippet:

function suppressBackspace(evt) {
    evt = evt || window.event;
    var target = evt.target || evt.srcElement;

    if (evt.keyCode == 8 && !/input|textarea/i.test(target.nodeName)) {
        return false;
    }
}

document.onkeydown = suppressBackspace;
document.onkeypress = suppressBackspace;

This should work in all browsers and it also filters out events that originated from an input or textarea element.

So if you want to use the backspace button for another action you can call the function there where it suppresses the default browser action.


to block backspace you can use this:

document.onkeydown = function(evt) {
    evt = evt || window.event;
    var keyCode = evt.keyCode;
    if (keyCode == 8) { // backspace
        return false;
    }
};


Backspace only makes the page go back if you don't have focus in a form field. With your Jsfiddle, I didn't have your issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜