本文主要是介绍javascript 禁制后退键(Backspace),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
function banBackSpace(e) {var ev = e || window.event;//获取event对象var obj = ev.target || ev.srcElement;//获取事件源var t = obj.type || obj.getAttribute('type');//获取事件源类型//获取作为判断条件的事件类型var vReadOnly = obj.readOnly;var vDisabled = obj.disabled;//处理undefined值情况vReadOnly = (vReadOnly == undefined) ? false : vReadOnly;vDisabled = (vDisabled == undefined) ? true : vDisabled;//当敲Backspace键时,事件源类型为密码或单行、多行文本的,//并且readOnly属性为true或disabled属性为true的,则退格键失效var flag1 = ev.keyCode == 8 && (t == "password" || t == "text" || t == "textarea") && (vReadOnly == true || vDisabled == true);//当敲Backspace键时,事件源类型非密码或单行、多行文本的,则退格键失效var flag2 = ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea";//判断if (flag2 || flag1) return false}//禁止退格键 作用于Firefox、Operadocument.onkeypress = banBackSpace;//禁止退格键 作用于IE、Chromedocument.onkeydown = banBackSpace;
这篇关于javascript 禁制后退键(Backspace)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!