if (evt.keyCode) {
if(evt.keyCode == 65){
window.location.hash = "#A";
}
if(evt.keyCode == 66){
window.location.hash = "#B";
}
if(evt.keyCode == 67){
window.location.hash = "#C";
}
if(evt.keyCode == 68){
window.location.hash = "#D";
}
if(evt.keyCode == 69){
window.location.hash = "#E";
}
if(evt.keyCode == 70){
window.location.hash = "#F";
}
if(evt.keyCode == 71){
window.location.hash = "#G";
}
if(evt.keyCode == 72){
window.location.hash = "#H";
}
if(evt.keyCode == 73){
window.location.hash = "#I";
}
if(evt.keyCode == 74){
window.location.hash = "#J";
}
if(evt.keyCode == 75){
window.location.hash = "#K";
}
if(evt.keyCode == 76){
window.location.hash = "#L";
}
if(evt.keyCode == 77){
window.location.hash = "#M";
}
if(evt.keyCode == 78){
window.location.hash = "#N";
}
if(evt.keyCode == 79){
window.location.hash = "#O";
}
if(evt.keyCode == 80){
window.location.hash = "#P";
}
if(evt.keyCode == 81){
window.location.hash = "#Q";
}
if(evt.keyCode == 82){
window.location.hash = "#R";
}
if(evt.keyCode == 83){
window.location.hash = "#S";
}
if(evt.keyCode == 84){
window.location.hash = "#T";
}
if(evt.keyCode == 85){
window.location.hash = "#U";
}
if(evt.keyCode == 86){
window.location.hash = "#V";
}
if(evt.keyCode == 87){
window.location.hash = "#W";
}
if(evt.keyCode == 88){
window.location.hash = "#X";
}
if(evt.keyCode == 89){
window.location.hash = "#Y";
}
if(evt.keyCode == 90){
window.location.hash = "#Z";
}
}
如何精简?
if (evt.keyCode) {
if (evt.keyCode > 64 && evt.keyCode < 91) {
window.location.hash = "#" + String.fromCharCode(evt.keyCode);
}
}
就这一行代码就行啦
window.location.hash = "#"+String.fromCharCode(evt.keyCode);