我的代码处于活动状态时出现了一个奇怪的问题,只有当我点击另一个链接然后切换标签时才出现这个问题:http://fiddle.jshell.net/3nafff9d/
要复制我的问题,您必须执行以下操作:
以 769宽度运行上面的JSFiddle,以显示菜单。
请注意,所有“有效”列表项均为蓝色,默认情况下,您切换到的所有选项卡上都会选中给我呼叫链接。
现在点击日志链接的下一个链接,直到您看到蓝色的活动状态颜色。
现在点击另一个标签页,您会注意到菜单中已经删除了活动状态。现在没有项目是蓝色的。
现在我的问题是:如何更改代码,以便即使切换标签或选择其他列表项也会记住活动状态颜色:蓝色?
JS:
//Selection Active
$('.sidebar-nav a').click(function() {
$('a').removeClass();
$(this).addClass('active');
});
//Selection Active
$('.sidebar-nav2 a').click(function() {
$('a').removeClass();
$(this).addClass('active');
});
//Selection Active
$('.sidebar-nav3 a').click(function() {
$('a').removeClass();
$(this).addClass('active');
});
//Selection Active
$('.sidebar-nav4 a').click(function() {
$('a').removeClass();
$(this).addClass('active');
});
//Custom Toggle
$('input[name="intervaltype"]').click(function () {
$('#Custom').toggleClass('active');
$('#Types').toggleClass('active');
});
//Custom Toggle
$('input[name="intervaltype2"]').click(function () {
$('#Custom2').toggleClass('active');
$('#Types2').toggleClass('active');
});
//Custom Toggle
$('input[name="intervaltype3"]').click(function () {
$('#Custom3').toggleClass('active');
$('#Types3').toggleClass('active');
});
//Custom Toggle
$('input[name="intervaltype4"]').click(function () {
$('#Custom4').toggleClass('active');
$('#Types4').toggleClass('active');
});
//Check and Uncheck
$(".checkAll").click(function () {
$(".check").prop('checked', $(this).prop('checked'));
});
//Check and Uncheck Below-2
$(".checkBelow").click(function () {
$(".checkother").prop('checked', $(this).prop('checked'));
});
//Custom Check and Uncheck All-2
$(".checkAll2").click(function () {
$(".check2").prop('checked', $(this).prop('checked'));
});
//Check and Uncheck Below-2
$(".checkBelow2").click(function () {
$(".checkother2").prop('checked', $(this).prop('checked'));
});
//Check and Uncheck 3
$(".checkAll3").click(function () {
$(".check3").prop('checked', $(this).prop('checked'));
});
//Check and Uncheck Below-3
$(".checkBelow3").click(function () {
$(".checkother3").prop('checked', $(this).prop('checked'));
});
//Custom Check and Uncheck All-4
$(".checkAll4").click(function () {
$(".check4").prop('checked', $(this).prop('checked'));
});
//Check and Uncheck Below-4
$(".checkBelow4").click(function () {
$(".checkother4").prop('checked', $(this).prop('checked'));
});
//Selection Active
$( '.servicelist a' ).on('shown.bs.tab', function(e){
if ( !$(this).hasClass( 'active' ) ) {
$( '.servicelist .active' ).removeClass( 'active' );
$( '.servicelist a[href="' + $(this).attr( 'href' ) + '"]' ).parent().addClass( 'active' );
}
});
HTML
<div id="sidebar-wrapper2">
<!-- Sidebar -->
<div class="topfilter">
<div class="tabber">
<!-- Nav tabs -->
<ul class="servicelist" role="tablist">
<li class="col-md-3 col-xs-3 serviceop active" role="presentation">
<a href="#construction" aria-controls="construction" role="tab" data-toggle="tab">
<i class="fa-2x fa fa-home"></i>
</a>
</li>
<li class="serviceop col-md-3 col-xs-3" role="presentation">
<a href="#precon" aria-controls="precon" role="tab" data-toggle="tab">
<i class="fa-2x fa fa-info-circle"></i>
</a>
</li>
<li class="col-md-3 col-xs-3 serviceop" role="presentation">
<a href="#generalcon" aria-controls="generalcon" role="tab" data-toggle="tab">
<i class="fa-2x fa fa-briefcase"></i>
</a>
</li>
<li class="col-md-3 col-xs-3 serviceop" role="presentation">
<a href="#builddesign" aria-controls="builddesign" role="tab" data-toggle="tab">
<i class="fa-2x fa fa-bullhorn"></i>
</a>
</li>
</ul>
</div> <!-- End Tabber-->
</div><!-- End Topfilter-->
</div> <!-- End sidebar-wrapper 2-->
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane fade in active" id="construction">
<div id="sidebar-wrapper3">
<li class="toppy" role="tab" style="list-style:none;">
<label data-target="#Custom"> <span class="customtext">Choose</span>
<input name="intervaltype" class="check checkAll chix" type="checkbox" />
</label>
</li>
</div>
<div class="tab-content" id="sidebar-wrapper">
<div role="tabpanel" class="tab-pane" id="Custom">
<ul class="sidebar-nav">
<form role="form">
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" class="check checkBelow"> Settings
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="check checkother"> Log out
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="check checkother">Profile
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="check checkother"> Account
</label>
</div>
</div><!-- End Form-group -->
</form>
</ul> <!-- End Sidebar Nav-->
</div> <!-- End Tab Panel-->
<div role="tabpanel" class="tab-pane active" id="Types">
<ul class="sidebar-nav">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="active highlight" data-toggle="collapse" data-parent="#accordion" href="#collapseZero"><span class="iconpad glyphicon glyphicon glyphicon-asterisk">
</span>Call Me</a>
</h4>
</div><!-- End Panel-Heading -->
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="highlight" data-toggle="collapse" data-parent="#accordion" href="#collapseOne"><span class="iconpad glyphicon glyphicon-user">
</span>Log</a>
</h4>
</div> <!-- End Panel-Heading -->
<div id="collapseOne" class="panel-collapse collapse in">
<li>
<a class="highlight" href="#/content">
Loglines
</a>
</li>
</div> <!-- End CollapseOne -->
<div class="bottomfilter">
<a href="#" class="filtertitle">Terms</a>
</div> <!-- End Bottom Filter -->
</div> <!-- End Panel Default in Accordion -->
</div><!-- End Accordion -->
</div> <!-- End Panel Default -->
</ul> <!-- End Sidebar Nav-->
</div> <!-- End Types 1 -->
</div> <!-- End SideBar Wrapper 1 -->
</div> <!-- End Tab Panel -->
<div role="tabpanel" class="tab-pane fade" id="precon">
<div id="sidebar-wrapper3">
<li class="toppy" role="tab" style="list-style:none;">
<label data-target="#Custom2"> <span class="customtext">Choose</span>
<input name="intervaltype2" class="check2 checkAll2 chix" type="checkbox" />
</label>
</li>
</div>
<div class="tab-content" id="sidebar-wrapper">
<div role="tabpanel" class="tab-pane" id="Custom2">
<ul class="sidebar-nav2">
<form role="form">
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" class="check2 checkBelow2"> Settingz
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="check2 checkother2"> Log outz
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="check2 checkother2">Profile
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="check2 checkother2"> Account
</label>
</div>
</div><!-- End Form-group -->
</form>
</ul> <!-- End Sidebar Nav-->
</div> <!-- End Tab Panel-->
<div role="tabpanel" class="tab-pane active" id="Types2">
<ul class="sidebar-nav2">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="active" data-toggle="collapse" data-parent="#accordion2" href="#bcollapseZero"><span class="iconpad glyphicon glyphicon glyphicon-asterisk">
</span>Call Mez</a>
</h4>
</div><!-- End Panel-Heading -->
<div class="panel-group" id="accordion2">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="highlight" data-toggle="collapse" data-parent="#accordion2" href="#bcollapseOne"><span class="iconpad glyphicon glyphicon-user">
</span>Logz</a>
</h4>
</div> <!-- End Panel-Heading -->
<div id="bcollapseOne" class="panel-collapse collapse in">
<li>
<a class="highlight" href="#/content">
Loglinez
</a>
</li>
</div> <!-- End CollapseOne -->
<div class="bottomfilter">
<a href="#" class="filtertitle">Termz</a>
</div> <!-- End Bottom Filter -->
</div> <!-- End Panel Default in Accordion -->
</div><!-- End Accordion -->
</div> <!-- End Panel Default -->
</ul> <!-- End Sidebar Nav-->
</div> <!-- End Types 1 -->
</div> <!-- End SideBar Wrapper 1 -->
</div> <!-- End Tab Panel -->
<div role="tabpanel" class="tab-pane fade" id="generalcon">
<div id="sidebar-wrapper3">
<li class="toppy" role="tab" style="list-style:none;">
<label data-target="#Custom3"> <span class="customtext">Choose</span>
<input name="intervaltype3" class="check3 checkAll3 chix" type="checkbox" />
</label>
</li>
</div>
<div class="tab-content" id="sidebar-wrapper">
<div role="tabpanel" class="tab-pane" id="Custom3">
<ul class="sidebar-nav3">
<form role="form">
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" class="check3 checkBelow3"> Settingz
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="check3 checkother3"> Log outz
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="check3 checkother3">Profile
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="check3 checkother3"> Account
</label>
</div>
</div><!-- End Form-group -->
</form>
</ul> <!-- End Sidebar Nav-->
</div> <!-- End Tab Panel-->
<div role="tabpanel" class="tab-pane active" id="Types3">
<ul class="sidebar-nav3">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="active" data-toggle="collapse" data-parent="#accordion3" href="#ccollapseZero"><span class="iconpad glyphicon glyphicon glyphicon-asterisk">
</span>Call Mez</a>
</h4>
</div><!-- End Panel-Heading -->
<div class="panel-group" id="accordion3">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="highlight" data-toggle="collapse" data-parent="#accordion3" href="#ccollapseOne"><span class="iconpad glyphicon glyphicon-user">
</span>Logz</a>
</h4>
</div> <!-- End Panel-Heading -->
<div id="ccollapseOne" class="panel-collapse collapse in">
<li>
<a class="highlight" href="#/content">
Loglinez
</a>
</li>
</div> <!-- End CollapseOne -->
<div class="bottomfilter">
<a href="#" class="filtertitle">Termz</a>
</div> <!-- End Bottom Filter -->
</div> <!-- End Panel Default in Accordion -->
</div><!-- End Accordion -->
</div> <!-- End Panel Default -->
</ul> <!-- End Sidebar Nav-->
</div> <!-- End Types 1 -->
</div> <!-- End SideBar Wrapper 1 -->
</div> <!-- End Tab Panel -->
<div role="tabpanel" class="tab-pane fade" id="builddesign">
<div id="sidebar-wrapper3">
<li class="toppy" role="tab" style="list-style:none;">
<label data-target="#Custom4"> <span class="customtext">Choose</span>
<input name="intervaltype4" class="check4 checkAll4 chix" type="checkbox" />
</label>
</li>
</div>
<div class="tab-content" id="sidebar-wrapper">
<div role="tabpanel" class="tab-pane" id="Custom4">
<ul class="sidebar-nav4">
<form role="form">
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" class="check4 checkBelow4"> Settings
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="check4 checkother4"> Log out
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="check4 checkother4">Profile
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="check4 checkother4"> Account
</label>
</div>
</div><!-- End Form-group -->
</form>
</ul> <!-- End Sidebar Nav-->
</div> <!-- End Tab Panel-->
<div role="tabpanel" class="tab-pane active" id="Types4">
<ul class="sidebar-nav4">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="active highlight" data-toggle="collapse" data-parent="#accordion4" href="#dcollapseZero"><span class="iconpad glyphicon glyphicon glyphicon-asterisk">
</span>Call Me</a>
</h4>
</div><!-- End Panel-Heading -->
<div class="panel-group" id="accordion4">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="highlight" data-toggle="collapse" data-parent="#accordion4" href="#dcollapseOne"><span class="iconpad glyphicon glyphicon-user">
</span>Log</a>
</h4>
</div> <!-- End Panel-Heading -->
<div id="dcollapseOne" class="panel-collapse collapse in">
<li>
<a class="highlight" href="#/content">
Loglines
</a>
</li>
</div> <!-- End CollapseOne -->
<div class="bottomfilter">
<a href="#" class="filtertitle">Terms</a>
</div> <!-- End Bottom Filter -->
</div> <!-- End Panel Default in Accordion -->
</div><!-- End Accordion -->
</div> <!-- End Panel Default -->
</ul> <!-- End Sidebar Nav-->
</div> <!-- End Types 1 -->
</div> <!-- End SideBar Wrapper 1 -->
</div> <!-- End Tab Panel -->
</div> <!-- End Tabs Content -->
CSS:
a {outline:none !important;}
html,
body,
.wrap {
height: 100%;
}
.wrap {
box-sizing: border-box;
}
form {
height: 100%
}
.wrap {
min-height: 100%;
height: auto !important;
height: 100%;
margin-bottom: -60px;
/* for sticky footer to not go below page */
/* for sticky header to not overlap content */
}
.push,
.footer {
height: 60px;
}
.footer {
background-color: #ebebeb;
height: 60px;
width: 100%;
position: fixed;
bottom: 0;
}
.content {
position: absolute;
width: 100%;
top: 120px;
background-color: yellow;
z-index: 0;
}
#sidebar-wrapper {
z-index: 1000;
position: fixed;
left: 142px;
height:95%;
width: 0; bottom:0; top:65px;
height:auto; border-top:1px solid #fff;
margin-left: -142px;
overflow-y: auto; overflow-x:hidden;
background: #eee;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
#sidebar-wrapper2 {
z-index: 1000;
position: fixed;
left: 142px;
top:0;
margin-bottom: 0px;
width:100%;
min-height:55px; height:auto;
margin-left: -142px;
background: #eee;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
#sidebar-wrapper3 {
z-index: 1000;
position: fixed;
left: 142px;
top:35px;
margin-bottom: 0px;
width:auto;
min-height:55px; height:auto;
margin-left: -142px;
background: #eee;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
.filtertitle { position:relative; display:inline-block; color:#000; width:auto; }
.topfilter { max-width:100%; min-width:100%; position:relative; display:inline-block; min-height:65px; }
.tabber {bottom:0px !important; height:auto; left:0; position:relative; display:inline-block; border-bottom: solid #fff 1px; }
.customtext {color:#666;}
.toppy {bottom:0px !important; text-indent:9px; }
.bottomfilter {border-top: solid #666 1px; width:100%; height:auto; position:relative; display:block; text-align:left; background-color:#eee; }
.checkbox {margin-left:25px !important;}
.dropdown { margin-top:15%; display:inline-block; }
.dropdown a {margin-left:20px; min-width:100% !important; }
.caret {text-align:right !important; position:relative;}
#wrapper.toggled #sidebar-wrapper {
width: 200px;
}
#wrapper.toggled #sidebar-wrapper2 {
width: 200px;
}
#page-content-wrapper {
width: 100%;
position: absolute;
padding: 15px;
}
#wrapper.toggled #page-content-wrapper {
position: absolute;
margin-right: -200px;
}
@media screen and (min-width:768px) {
#wrapper {
padding-left: 300px;
}
#wrapper.toggled {
padding-left: 0;
}
#wrapper.toggled #sidebar-wrapper {
width: 0;
}
#page-content-wrapper {
padding: 20px;
position: relative;
}
#wrapper.toggled #page-content-wrapper {
position: relative;
margin-right: 0;
}
}
@media screen and (max-width:1526px) {
.content,
.footer {
width: 82%;
right: 0;
}
.header {width:82%;}
#sidebar-wrapper {
width: 18%;
}
#sidebar-wrapper2 {
width: 18%;
}
}
@media screen and (max-width:1059px) {
.content,
.footer {
width: 80%;
right: 0;
}
.header {width:80%;}
#sidebar-wrapper {
width: 20%;
}
#sidebar-wrapper2 {
width: 20%;
}
}
@media screen and (min-width:1527px) {
.content,
.footer {
width: 85%;
right: 0;
}
.header {width:85%;}
#sidebar-wrapper {
width: 15%;
}
#sidebar-wrapper2 {
width: 15%;
}
}
@media screen and (max-width:991px) {
.content,
.footer {
width: 100%;
right: 0;
}
.filtertitle {display:none;}
#sidebar-wrapper {
width:0;
}
#sidebar-wrapper2 {
width:0;
}
.topfilter {width:0; display:none;}
.dropdown a {display:none;}
}
.iconpad { margin-right:10px; }
.panel-body { padding:0px; }
.panel-body table tr td { padding-left: 15px }
.panel-body .table {margin-bottom: 0px; }
.panel-title a:hover {color:#333; text-decoration:none; background:none !important;}
.panel-title a:focus {color:#39f; text-decoration:none; background:none !important;}
.panel-title a {text-decoration:none; background:none !important;}
/* Service list - Top Sidebar Icon Nav */
.servicelist { list-style:none; padding:0; }
.servicelist a{ background:none !important; text-decoration:none; color:#333; list-style:none; }
.servicelist a :hover { color:#bdc3c7; }
.servicelist > li.active > a .fa-2x {
color:#f90;
cursor: pointer;
}
/* Sidebar Styles */
.sidebar-nav {
position: absolute;
top: 0px;
width:inherit; min-width:100%;
margin: 0;
padding: 0;
list-style: none;
}
.sidebar-nav li {
text-indent: 20px;
line-height: 40px;
}
.sidebar-nav li a {
display: block;
text-decoration: none;
color: #999999;
}
.sidebar-nav li a:hover {
text-decoration: none;
color: #666;
background:#bdc3c7;
}
.sidebar-nav li a:active,
.sidebar-nav li a:focus {
text-decoration: none;
}
.sidebar-nav a.active {
color:#39f; background-color:#eee; }
/* Sidebar Styles 2*/
.sidebar-nav2 {
position: absolute;
top: 0px;
width:inherit; min-width:100%;
margin: 0;
padding: 0;
list-style: none;
}
.sidebar-nav2 li {
text-indent: 20px;
line-height: 40px;
}
.sidebar-nav2 li a {
display: block;
text-decoration: none;
color: #999999;
}
.sidebar-nav2 li a:hover {
text-decoration: none;
color: #666;
background:#bdc3c7;
}
.sidebar-nav2 li a:active,
.sidebar-nav2 li a:focus {
text-decoration: none;
}
.sidebar-nav2 a.active {
color:#39f; background-color:#eee; }
/* Sidebar Styles 3*/
.sidebar-nav3 {
position: absolute;
top: 0px;
width:inherit; min-width:100%;
margin: 0;
padding: 0;
list-style: none;
}
.sidebar-nav3 li {
text-indent: 20px;
line-height: 40px;
}
.sidebar-nav3 li a {
display: block;
text-decoration: none;
color: #999999;
}
.sidebar-nav3 li a:hover {
text-decoration: none;
color: #666;
background:#bdc3c7;
}
.sidebar-nav3 li a:active,
.sidebar-nav3 li a:focus {
text-decoration: none;
}
.sidebar-nav3 a.active {
color:#39f; background-color:#eee; }
/* Sidebar Styles 3*/
.sidebar-nav4 {
position: absolute;
top: 0px;
width:inherit; min-width:100%;
margin: 0;
padding: 0;
list-style: none;
}
.sidebar-nav4 li {
text-indent: 20px;
line-height: 40px;
}
.sidebar-nav4 li a {
display: block;
text-decoration: none;
color: #999999;
}
.sidebar-nav4 li a:hover {
text-decoration: none;
color: #666;
background:#bdc3c7;
}
.sidebar-nav4 li a:active,
.sidebar-nav4 li a:focus {
text-decoration: none;
}
.sidebar-nav4 a.active {
color:#39f; background-color:#eee; }
答案 0 :(得分:0)
这种情况正在发生,因为您的代码将事件侦听器绑定到这些链接中的每一个,然后在擦除任何类的所有链接后使用$(this)
来应用活动类。虽然它在逐个选项卡的基础上工作,但它只会将该类添加到该特定选项卡中的特定元素。因此,当用户点击第一个&#34; Call Me&#34;链接,它标记为活动状态,当他们切换标签时,一个全新的&#34; Call Me&#34;显示了链接,但由于事件监听器仅将类添加到被单击的项目,因此它没有添加活动类。
您可以简单地将活动类应用于所有其他标签下的相同元素,例如,当用户点击第一个&#34; Call Me&#34;链接,让所有&#34;打电话给我&#34;链接活跃。
如果您确实想要记住状态(哪些链接处于活动状态等),那么最佳路线将是存储此信息并基于此执行逻辑。存储位置取决于页面的运行方式。如果它只是短期的,你可以简单地将它保存在变量或会话存储中。如果它将跨越多个浏览会话,那么您可以去本地存储。
修改强>
如果我理解正确,那么可能更容易将callMe
课程添加到所有&#34; Call Me&#34;链接,以及log
类到所有&#34; Log&#34;链接,然后删除:
//Selection Active
$('.sidebar-nav a').click(function() {
$('a').removeClass();
$(this).addClass('active');
});
//Selection Active
$('.sidebar-nav2 a').click(function() {
$('a').removeClass();
$(this).addClass('active');
});
//Selection Active
$('.sidebar-nav3 a').click(function() {
$('a').removeClass();
$(this).addClass('active');
});
//Selection Active
$('.sidebar-nav4 a').click(function() {
$('a').removeClass();
$(this).addClass('active');
});
只需使用:
//Selection Active
$('.callMe').click(function() {
$('.log').removeClass('active');
$('.callMe').addClass('active');
});
//Selection Active
$('.log').click(function() {
$('.callMe').removeClass('active');
$('.log').addClass('active');
});