JQuery防止退格键网页后退

来源:岁月联盟 编辑:exp 时间:2012-02-29

有时我们需要防止退格键的网页后退,一般情况下最好不要这么用,因为对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键的代码可以通过这里http://api.jquery.com/event.which/
        希望对您Web开发有帮助。
       
        您可能感兴趣的文章:
     
        使用Jquery,CSS3实现像GooglePlus那样的圆圈效果http://www.2cto.com/kf/201202/121327.html

摘自 PetterLiu