我希望在文本框聚焦时禁用网页刷新 我正在使用的代码是适用于其他快捷方式的代码 除了:
BTW我想通过简单的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禁用它时 但它不起作用,任何见解都会受到高度赞赏