バックホール禁止ボタンBackSpaceのJavaScript実現(IE、Chrome、Firefox、Opera対応)

4150 ワード

 1 var forbidBackSpace = function (e) {
 2     //   event  
 3     var ev = e || window.event;
 4     //      
 5     var obj = ev.target || ev.srcElement;
 6     //        
 7     var t = obj.type || obj.getAttribute('type');
 8     //              
 9     var vReadOnly = obj.readOnly;
10     var vDisabled = obj.disabled;
11     //   undefined    
12     vReadOnly = (vReadOnly == undefined) ? false : vReadOnly;
13     vDisabled = (vDisabled == undefined) ? true : vDisabled;
14     //   Backspace  ,           、     , 
15     //   readOnly   true disabled   true ,       
16     var flag1 = ev.keyCode == 8 && (t == "password" || t == "text" || t == "textarea") && (vReadOnly == true || vDisabled == true);
17     //   Backspace  ,           、     ,       
18     var flag2 = ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea";
19     //    
20     if (flag1 || flag2) { return false; }
21 }
22 
23 //          Firefox、Opera
24 document.onkeypress = forbidBackSpace;
25 //           IE、Chrome
26 document.onkeydown = forbidBackSpace;