Home  >  Article  >  Web Front-end  >  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

WBOY
WBOYOriginal
2016-05-16 18:28:45967browse

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;
}
}
}
}
Statement:
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