This article mainly introduces how JavaScript disables the Backspace key, which can help you solve the problem of "rolling back to the previous page by pressing the Backspace key in the read-only input box in IE, if you are interested, you can refer to the findings in IE today. when you use the readonly = "readonly" attribute to set the text box to read-only
There is a strange problem: if the cursor enters the read-only text box, and then press the Backspace key, it will jump to the previous page, the effect is like clicking the back button of the browser to return to the previous page, this problem does not occur in Firefox and google. to solve this problem, write the following solution: if the text box is read-only
Disable the Backspace key.
The code is as follows:
// Handle keyboard events do not allow Backspace passwords or single-line, multi-line text boxes except function banBackSpace (e) {var ev = e | window. event; // Obtain The event object var obj = ev.tar get | ev. srcElement; // Obtain the event source var t = obj. type | obj. getAttribute ('type'); // Obtain the event source type // Obtain the event type var vReadOnly = obj. getAttribute ('readonly'); // when processing null values, vReadOnly = (vReadOnly = "")? False: vReadOnly; // when you press the Backspace key, if the event source type is password or single-line or multi-line text, // And The readonly attribute is true or the enabled attribute is false, var flag1 = (ev. keyCode = 8 & (t = "password" | t = "text" | t = "textarea") & vReadOnly = "readonly ")? True: false; // when you press the Backspace key, if the event source type is non-password or single-line or multi-line text, the return key fails to be var flag2 = (ev. keyCode = 8 & t! = "Password" & t! = "Text" & t! = "Textarea ")? True: false; // judge if (flag2) {return false;} if (flag1) {return false;} window. onload = function () {// disable the backend key for Firefox and Opera document. onkeypress = banBackSpace; // disable the backend key to act on IE and Chrome document. onkeydown = banBackSpace ;}
With this processing, you can easily solve the problem of "rolling back to the previous page by pressing the Backspace key in IE in the read-only input box.
I hope you will like this article and continue to pay attention to the updated articles in the mini series.