这里是bootstrap标签:
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li style="padding: 20px" role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a></li>
<li style="padding: 20px" role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a></li>
<li style="padding: 20px" role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">Messages</a></li>
<li style="padding: 20px" role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Settings</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">
<h1>Hello World</h1>
</div>
<div role="tabpanel" class="tab-pane" id="profile">Hello</div>
<div role="tabpanel" class="tab-pane" id="messages">Some text</div>
<div role="tabpanel" class="tab-pane" id="settings">Second text</div>
</div>
</div>
</div>
并且当前标签使用bootstrap javascript自动激活类。
所以我想通过jquery在当前标签上添加边框:
$('li').click(function () {
if ($(this).hasClass('active')) {
$(this).addClass('border-add')
}
})
我不能这样做。因为这段代码很糟糕。如果单击其中一个选项卡边框将显示在当前选项卡中。但如果我将点击另一个标签,我点击的第一个标签边框将不会消失。 伙计们向我解释如何实现它。提前谢谢!
答案 0 :(得分:3)
这可以在没有JQuery的情况下使用类
来完成li.active{
border: 1px solid black;
}
/* Latest compiled and minified CSS included as External Resource*/
/* Optional theme */
@import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
body {
margin: 10px;
}
li.active{
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li style="padding: 20px" role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a></li>
<li style="padding: 20px" role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a></li>
<li style="padding: 20px" role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">Messages</a></li>
<li style="padding: 20px" role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Settings</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">
<h1>Hello World</h1>
</div>
<div role="tabpanel" class="tab-pane" id="profile">Hello</div>
<div role="tabpanel" class="tab-pane" id="messages">Some text</div>
<div role="tabpanel" class="tab-pane" id="settings">Second text</div>
</div>
</div>
</div>
答案 1 :(得分:1)
你可能只是在那里。听起来你需要做的就是在添加之前删除border-add
:
$('li').click(function () {
$('li.border-add').removeClass('border-add'); // remove from the one that has it first
if ($(this).hasClass('active')) {
$(this).addClass('border-add'); // then add the new one
}
})