Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 在开关盒内循环以最小化代码?_Jquery_Loops_Switch Statement - Fatal编程技术网

Jquery 在开关盒内循环以最小化代码?

Jquery 在开关盒内循环以最小化代码?,jquery,loops,switch-statement,Jquery,Loops,Switch Statement,我想知道是否有可能通过创建函数、循环或任何其他技巧来最小化此代码,以避免代码块26行 $("#inputText").bind('keyup',function(objEvent){ switch(objEvent.keyCode) { case 65: $("div:contains('A')").attr("class", styledClass); break; case 66: $("div:contains('B')").attr("class", styledClass); brea

我想知道是否有可能通过创建函数、循环或任何其他技巧来最小化此代码,以避免代码块26行

$("#inputText").bind('keyup',function(objEvent){
switch(objEvent.keyCode)
{
case 65: $("div:contains('A')").attr("class", styledClass); break;
case 66: $("div:contains('B')").attr("class", styledClass); break;
...
case 90: $("div:contains('Z')").attr("class", styledClass);
}
})

使用
字符串。fromCharCode

$("#inputText").bind('keyup',function(objEvent){
    var letter = String.fromCharCode(objEvent.keyCode);
    $("div:contains('" + letter + "')").attr("class", styledClass);
})

令人惊叹的!很有效,谢谢。我还忘了在引号之间加上“styledClass”。太好了!请点击答案附近的灰色V符号,将答案设置为正确