首页 新闻 搜索 专区 学院

js代码精简

0
悬赏园豆:50 [已解决问题] 解决于 2013-05-06 08:30
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";
}
}
如何精简?
js
Flameman的主页 Flameman | 初学一级 | 园豆:45
提问于:2013-05-04 17:20
< >
分享
最佳答案
0

        if (evt.keyCode) {
            if (evt.keyCode > 64 && evt.keyCode < 91) {
                window.location.hash = "#" + String.fromCharCode(evt.keyCode);
                }
         }

收获园豆:35
yyutudou | 小虾三级 |园豆:997 | 2013-05-04 17:52
其他回答(2)
0

就这一行代码就行啦

window.location.hash = "#"+String.fromCharCode(evt.keyCode);

收获园豆:15
淑女丫丫 | 园豆:93 (初学一级) | 2013-05-04 17:48
0
chenping2008 | 园豆:9836 (大侠五级) | 2013-05-05 12:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册