Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IE浏览器后退: 禁止backspace键使用 #184

Open
ly2011 opened this issue Sep 23, 2019 · 0 comments
Open

IE浏览器后退: 禁止backspace键使用 #184

ly2011 opened this issue Sep 23, 2019 · 0 comments
Labels

Comments

@ly2011
Copy link
Owner

ly2011 commented Sep 23, 2019

function banBackSpace(e){
  var ev = e || window.event;
  //各种浏览器下获取事件对象
  var obj = ev.relatedTarget || ev.srcElement || ev.target ||ev.currentTarget;
  //按下Backspace键
  if(ev.keyCode == 8){
    var tagName = obj.nodeName //标签名称
    //如果标签不是input或者textarea则阻止Backspace
    if(tagName!='INPUT' && tagName!='TEXTAREA'){
      return stopIt(ev);
    }
    var tagType = obj.type.toUpperCase();//标签类型
    //input标签除了下面几种类型,全部阻止Backspace
    if(tagName=='INPUT' && (tagType!='TEXT' && tagType!='TEXTAREA' && tagType!='PASSWORD')){
      return stopIt(ev);
    }
    //input或者textarea输入框如果不可编辑则阻止Backspace
    if((tagName=='INPUT' || tagName=='TEXTAREA') && (obj.readOnly==true || obj.disabled ==true)){
      return stopIt(ev);
    }
  }
}
function stopIt(ev){
  if(ev.preventDefault ){
    //preventDefault()方法阻止元素发生默认的行为
    ev.preventDefault();
  }
  if(ev.returnValue){
    //IE浏览器下用window.event.returnValue = false;实现阻止元素发生默认的行为
    ev.returnValue = false;
  }
  return false;
}

$(function(){
  //实现对字符码的截获,keypress中屏蔽了这些功能按键
  document.onkeypress = banBackSpace;
  //对功能按键的获取
  document.onkeydown = banBackSpace;
})

参考文档

  1. IE浏览器后退: 禁止backspace键使用
  2. IE11 如何屏蔽backspace返回上一页功能?
@ly2011 ly2011 added the 转载 label Sep 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant