Home > Web Front-end > JS Tutorial > js disable the backspace key when the read-only text box gains focus_javascript tips

js disable the backspace key when the read-only text box gains focus_javascript tips

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 18:28:45
Original
1045 people have browsed it

Sometimes it is inevitable to use a read-only text box, but today I discovered that the read-only text box has a flaw. When the mouse focus is in the text box, pressing the backspace key will return to the previous page. This problem It's a bit annoying. The user doesn't know whether he can input it. If he sees the text box and wants to change the content in it, and clicks it, all the previously filled in data may be lost. Therefore, I wrote a method for everyone who needs it. When inserting <script></script>, it will be kept to your liking.

Copy code The code is as follows:

document.documentElement.onkeydown = function(evt){
var b = !!evt, oEvent = evt || window.event;
if (oEvent.keyCode == 8) {
var node = b ? oEvent.target : oEvent.srcElement;
var reg = /^(input|textarea)$/i, regType = /^(text|textarea)$/i;
if (!reg.test(node.nodeName) || !regType.test(node. type) || node.readOnly || node.disabled) {
if (b)
{
oEvent.stopPropagation();
}
else
{
oEvent .cancelBubble = true;
oEvent.keyCode = 0;
oEvent.returnValue = false;
}
}
}
}
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template