使用jQuery创建一个干净的

时间:2013-12-24 18:20:48

标签: javascript jquery html

我基本上希望使用基于以下HTML代码的jQuery以最干净的方式执行switch()if()

<ul class="nav nav-tabs">
    <li class="active"><a href="#core" data-toggle="tab">Core Settings</a></li>
    <li><a href="#parallel" data-toggle="tab">Parallel settings</a></li>
    <li><a href="#cron_settings" data-toggle="tab">Cron limit settings</a></li>
</ul>

这是我正在尝试执行的一个示例

switch(".nav nav-tabs li > active") {
    case "#core":

    break;
    case "#parallel":

    break;
    case "#cron_settings":

    break;
}

这种语法显然不是正确的方法,它只是我希望如何能够执行此类switchif

的示例

那么,这样做最干净,最好的方法是什么?

更新编辑

我尝试在我的代码中使用以下回答无效

$(document).ready(function() {
    $("ul.nav.nav-tabs li.active a").each(function(this){
        switch(this.href) {
            case "#core":
                alert("you're using the core tab");
            break;
            case "#parallel":
                alert("you're using the parallel tab");
            break;
            case "#cron_settings":
                alert("you're using the cron settings tab");
            break;  
        }
     })
});

1 个答案:

答案 0 :(得分:4)

你需要稍微修改你的选择器,所以试试这个:

$("ul.nav.nav-tabs li.active a").each(function(){
    switch(this.href) {
       case ...
    }
});

在您的情况下,可能想要使用this.hash