Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.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
Javascript 简化jQuery开关情况_Javascript_Jquery_Switch Statement - Fatal编程技术网

Javascript 简化jQuery开关情况

Javascript 简化jQuery开关情况,javascript,jquery,switch-statement,Javascript,Jquery,Switch Statement,我使用的是JQVMap,我有我的代码,根据你点击的状态,它会在地图下方的一个div中显示该状态的信息。然而,我觉得有一种简化的、更有效的方法来设置代码,这样就不会有太多重复,尤其是当我开始添加更多的状态时。有吗 switch(code){ case"tx": $('#info-children').children(':not(#info-tx)').hide("slow"); $('#info-children').childre

我使用的是JQVMap,我有我的代码,根据你点击的状态,它会在地图下方的一个div中显示该状态的信息。然而,我觉得有一种简化的、更有效的方法来设置代码,这样就不会有太多重复,尤其是当我开始添加更多的状态时。有吗

switch(code){
        case"tx":
            $('#info-children').children(':not(#info-tx)').hide("slow");
            $('#info-children').children('#info-tx').show("slow");
            break;
        case"il":
            $('#info-children').children(':not(#info-il)').hide("slow");
            $('#info-children').children('#info-il').show("slow");
            break;
        case"fl":
            $('#info-children').children(':not(#info-fl)').hide("slow");
            $('#info-children').children('#info-fl').show("slow");
            break;
        case"ga":
            $('#info-children').children(':not(#info-ga)').hide("slow");
            $('#info-children').children('#info-ga').show("slow");
            break;
        case"pa":
            $('#info-children').children(':not(#info-pa)').hide("slow");
            $('#info-children').children('#info-pa').show("slow");
            break;
        default:
            $('#state-name').html(region);
            $('#info-children').children(':not(#info-uhoh)').hide("slow");
            $('#info-children').children('#info-uhoh').show("slow");
    }

我想你可以把它放在函数里,而不是全部写出来。动态创建语句

function(code){
            $('#info-children').children(':not(#info-'+ code +')).hide("slow");
            $('#info-children').children('#info-'+ code +').show("slow");
    }