【问题标题】:Simplify jQuery Switch Case [closed]简化 jQuery Switch 案例 [关闭]
【发布时间】:2015-02-14 04:27:09
【问题描述】:

我正在使用 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");
    }

【问题讨论】:

    标签: javascript jquery switch-statement


    【解决方案1】:

    我认为您可以将其放入函数中,而不是全部编写。动态创建语句

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

    【讨论】:

      猜你喜欢
      • 2021-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-18
      • 1970-01-01
      相关资源
      最近更新 更多