有时我们需要防止退格键的网页后退,一般情况下最好不要这么用,因为对UX体验不好。 下面是Js片段在IE9,Firebox 10.0.2 中测试过 : 

$(document).keydown(function (e) {
    var doPrevent;
    if (e.keyCode == 8) {
        var d = e.srcElement || e.target;
        if (d.tagName.toUpperCase() == 'INPUT' || d.tagName.toUpperCase() == 'TEXTAREA') {
            doPrevent = d.readOnly || d.disabled;
        }
        else
            doPrevent = true;
    }
    else
        doPrevent = false;

    if (doPrevent)
        e.preventDefault();
}); 

 

        关于如何确定Backspace键的代码可以通过这里.

        希望对您Web开发有帮助。
       
        您可能感兴趣的文章:
     
        使用Jquery,CSS3实现像GooglePlus那样的圆圈效果


作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-Petter Liu Blog

posted on 2012-02-27 15:38  PetterLiu  阅读(5287)  评论(0编辑  收藏  举报