Today found in IE browser, when the use of the readonly= "ReadOnly" property to set the text box to read-only <input type= "text" readonly= "readonly"/> A strange problem with is that if the cursor goes into a read-only text box and then presses the BACKSPACE key, it jumps to the previous page, as if clicking the Back button on the browser returns to the previous page, and there is no such problem with Firefox and Google. , to solve this problem, write a method such as the following, if the text box is read-only, then disables the backspace key . The
code is as follows:
/handling keyboard events prohibit back key (Backspace) password or single line, multiline text box exception function Banbackspace (e) {var ev = e | | window.event;// Get Event object var obj = Ev.target | | ev.srcelement;//get event source var t = Obj.type | |
Obj.getattribute (' type '); Get event Source Type//Get the event type as a condition of judgment var vreadonly = Obj.getattribute (' readonly '); Handle NULL value condition Vreadonly = (Vreadonly = = "")?
False:vreadonly; When the BACKSPACE key is hit, the event source type is a password or single line, multiline text,//And the ReadOnly property is True or the Enabled property is False, and the BACKSPACE key fails var flag1= (Ev.keycode = = 8 &&
(t== "Password" | | t== "TEXT" | | t== "TEXTAREA") && vreadonly== "ReadOnly")? The BACKSPACE key fails var flag2= (Ev.keycode = 8 && t!= "password" && t!= "text" backspace when the event source type is not a password or a single line, multi-line text, when the key is tapped &A
mp;& T!= "textarea")? True:false;
Judge if (Flag2) {return false;
} if (Flag1) {return false;
} window.onload=function () {//No Back key action on Firefox, Opera Document.onkeypress=banbackspace;
Prohibit the back key role in IE, Chrome document.onkeydown=banbackspace; }
With such processing, you can easily solve the "read-only input box under IE press the BACKSPACE key back to the previous page" problem.
I hope you like this article, continue to focus on the small series of updates and finishing articles.