gpt4 book ai didi

javascript - 单击非事件链接并切换选项卡时事件状态消失

转载 作者:行者123 更新时间:2023-11-28 05:22:26 24 4
gpt4 key购买 nike

我的代码的事件状态有一个奇怪的问题,只有当我点击另一个链接然后切换标签时才会出现:http://fiddle.jshell.net/3nafff9d/

enter image description here

要重现我的问题,您必须执行以下操作:

  1. 769 宽度 运行上面的 JSFiddle 以显示菜单。

  2. 请注意,所有“事件”列表项都是蓝色的,默认情况下,您切换到的所有选项卡上的调用我链接 都处于选中状态。

  3. 现在单击下一个链接 Log 链接,直到您看到蓝色的事件状态颜色。

  4. 现在单击另一个选项卡,您会注意到事件状态突然从菜单中消失了。现在没有元素是蓝色的。

现在我的问题是:如何更改代码,以便即使在切换选项卡或选择其他列表项时也能记住事件状态颜色:蓝色?

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; }

最佳答案

发生这种情况是因为您的代码将事件监听器绑定(bind)到这些链接中的每一个,然后在删除任何类的所有链接后使用 $(this) 应用事件类。虽然这在逐个选项卡的基础上起作用,但它只会将该类添加到该特定选项卡中的特定元素。因此,当用户单击第一个“调用我”链接时,它被标记为事件,当他们切换选项卡时,将显示一个全新的“调用我”链接,但由于事件监听器仅将类添加到被单击的元素,它没有添加事件类。

您可以简单地将事件类应用于所有其他选项卡下的相同元素,例如,当用户单击第一个“调用我”链接时,使所有“调用我”链接都处于事件状态。

如果您真的想记住状态(哪些链接处于事件状态等),那么最好的方法是存储此信息并基于此执行逻辑。将其存储在何处取决于您的页面应该如何运行。如果只是短期的,您可以简单地将其保存在变量或 session 存储中。如果它要跨越多个浏览 session ,那么您可以选择本地存储。

编辑:

如果我理解正确,那么向所有“调用我”链接添加一个 callMe 类,向所有“Log”添加一个 log 类可能会更容易"链接,然后删除:

//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');
});

关于javascript - 单击非事件链接并切换选项卡时事件状态消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38962416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com