我基本上想基于以下 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;
}
该语法显然不是正确的方法,它只是我希望如何执行此类 switch
或 if
的示例
那么,最干净、最简洁的方法是什么?
更新编辑
我尝试在代码中使用以下答案,但没有成功
$(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;
}
})
});
最佳答案
您需要稍微修改一下选择器,所以试试这个:
$("ul.nav.nav-tabs li.active a").each(function(){
switch(this.href) {
case ...
}
});
在您的情况下,您可能想使用this.hash
。
关于javascript - 使用 jQuery 创建一个干净的 if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20765228/