Javascript 简化jQuery开关情况
我使用的是JQVMap,我有我的代码,根据你点击的状态,它会在地图下方的一个div中显示该状态的信息。然而,我觉得有一种简化的、更有效的方法来设置代码,这样就不会有太多重复,尤其是当我开始添加更多的状态时。有吗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
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");
}