剛好有這個需求,上網找了一下,整理如下:
禁止backspace的回上一頁功能,但可以刪除文字內容。
document.onkeydown = check;
function check(e) {
var code;
if (!e) {var e = window.event;}
if (e.keyCode) {code = e.keyCode;}
else if (e.which) {code = e.which;}
if (((event.keyCode == 8) &&
((event.srcElement.type != "text" &&
event.srcElement.type != "textarea" &&
event.srcElement.type != "password") ||
event.srcElement.readOnly == true)) ||
((event.ctrlKey) && ((event.keyCode == 78) || (event.keyCode == 82))) ||
(event.keyCode == 116))
{
event.keyCode = 0;
event.returnValue = false;
}
return true;
}
完全禁止backspace功能
document.onkeydown = check;
function check(e) {
var code;
if(!e)
{
var e = window.event;
}
if(e.keyCode)
{
code = e.keyCode;
}
else if(e.which)
{
code = e.which;
}
if (code == 8)
{
code = 0;
return false;
}
return true;
}
禁止backspace的回上一頁功能,但可以刪除文字內容。
document.onkeydown = check;
function check(e) {
var code;
if (!e) {var e = window.event;}
if (e.keyCode) {code = e.keyCode;}
else if (e.which) {code = e.which;}
if (((event.keyCode == 8) &&
((event.srcElement.type != "text" &&
event.srcElement.type != "textarea" &&
event.srcElement.type != "password") ||
event.srcElement.readOnly == true)) ||
((event.ctrlKey) && ((event.keyCode == 78) || (event.keyCode == 82))) ||
(event.keyCode == 116))
{
event.keyCode = 0;
event.returnValue = false;
}
return true;
}
完全禁止backspace功能
document.onkeydown = check;
function check(e) {
var code;
if(!e)
{
var e = window.event;
}
if(e.keyCode)
{
code = e.keyCode;
}
else if(e.which)
{
code = e.which;
}
if (code == 8)
{
code = 0;
return false;
}
return true;
}
留言
張貼留言