我想在文本框聚焦时禁用网页刷新我正在使用的代码是这对其他快捷方式非常有用,除了:
顺便说一句,我想通过纯 JavaScript 没有 JQuery 和没有 JavaScript 框架来实现这一点。
document.onkeydown = keyDown //control chars
document.onkeypress = keyDown //any char/number
function keyDown(e) {
//74=j, 75=k, 78=n, 84=t
var forbiddenCtrlKeys = new Array(74,75,78, 84);
//37=left_arrow, 39=right_arrow
var forbiddenAltKeys = new Array(37,39);
//8=backspace, 116=F5
var forbiddenSingleKeys = new Array(8,116);
//per le textbox 116=F5
var forbiddenTextBoxKeys = new Array(116);
key = window.event.keyCode; //IE
if(window.event.ctrlKey){ //ctrl
isCtrl = true;
isAlt = false
checkVal(forbiddenCtrlKeys,window.event.keyCode);
}
else if(window.event.altKey){ //alt
isCtrl = false;
isAlt = true;
checkVal(forbiddenAltKeys,window.event.keyCode);
}
else{ //other
isCtrl = false;
isAlt = false;
var element = e.target.nodeName.toLowerCase();
var curr = document.activeElement
if(curr.tagName=="INPUT")
alert(curr.tagName);
if((element != 'input' && element != 'textarea') || $(e.target).attr("readonly")){
alert(window.event.keyCode);
checkVal(forbiddenSingleKeys,window.event.keyCode);
}
if(document.event.keyCode==116){
alert(window.event.keyCode);
document.event.keyCode=0;
checkVal(forbiddenTextBoxKeys,window.event.keyCode);
}
}
}
function checkVal(set,key){
for(i in set)
{
//case-insensitive comparison
if(String.fromCharCode(set[i]).toLowerCase() == String.fromCharCode(key).toLowerCase())
{
alert('This key combination has been disabled.');
window.event.returnValue = false;
window.event.keyCode = 0;
return true;
}
}
}
所以我在其他情况下试图实现的是每当我尝试按 F5 禁用它但它不起作用时,任何洞察力将不胜感激