显示/隐藏图标折叠所有div的问题



我有以下代码,其中包含一些嵌套的DIV。

DIV 具有加号和减号字体真棒图标来展开和折叠它们。

您可以使用页面顶部的按钮切换 DIV 的可见性。

蓝色按钮和/或加/减图标可以切换父项的可见性(ActivitiesAnimals & Nature)。

绿色按钮切换包含子标题(如award-medalevent等)的"子"div 的可见性。

我遇到的问题是,当涉及到使用绿色加号/减号图标切换子div 的可见性时,例如,如果您单击award-medal标题的绿色减号,它也会折叠divevent

我想知道是否有任何方法可以拆分那里的折叠功能,以便绿色切换按钮仍然像现在一样工作,但以便各个 gree 加/减图标控制与图标相关的每个div 的可见性。

我有点明白为什么代码像现在这样工作,但无法弄清楚我必须如何更改 JS 代码才能实现我想要做的事情。

$(document).ready(function() {
$('.toggleparent').addClass('toggle-open');
$('.heading > a').on('click', function(event){
	event.preventDefault();
	if ( $(this).find('i').hasClass('fa-minus') ) {
	  $(this).find('i').removeClass('fa-minus').addClass('fa-plus');
	  $(this).parent().parent().find('.submenu').css("display", "none");
	  $(this).parent().parent().find('.submenu').addClass('closed');
	  if ( $('.submenu.closed').length == $('.menu').length ) {
		$('.toggleparent').removeClass('toggle-open');
	  }
	}
	else if ( $(this).find('i').hasClass('fa-plus') ) {
	  $(this).find('i').removeClass('fa-plus').addClass('fa-minus');
	  $(this).parent().parent().find('.submenu').css("display", "block");
	  $(this).parent().parent().find('.submenu').removeClass('closed');
	  $('.toggleparent').addClass('toggle-open');
	}
});
$('.toggleparent').on('click', function(){
	if ( $(this).hasClass('toggle-open') ) {
	  $('.heading').each(function() {
		$(this).find('i').removeClass('fa-minus').addClass('fa-plus');
		$(this).parent().find('.submenu').css("display", "none");
		$(this).parent().parent().find('.submenu').addClass('closed');
	  });
	  $('.toggleparent').removeClass('toggle-open');
	}
	else{
	  $('.heading').each(function() {
		$(this).find('i').removeClass('fa-plus').addClass('fa-minus');
		$(this).parent().find('.submenu').css("display", "block");
		$(this).parent().parent().find('.submenu').removeClass('closed');
	  });
	  $('.toggleparent').addClass('toggle-open');
	}
});
});
$(document).ready(function() {
$('.togglechild').addClass('toggle-open');
$('.subheading > a').on('click', function(event){
	event.preventDefault();
	if ( $(this).find('i').hasClass('fa-minus') ) {
	  $(this).find('i').removeClass('fa-minus').addClass('fa-plus');
	  $(this).parent().parent().find('.indent').css("display", "none");
	  $(this).parent().parent().find('.indent').addClass('closed');
	  if ( $('.indent.closed').length == $('.menu').length ) {
		$('.togglechild').removeClass('toggle-open');
	  }
	}
	else if ( $(this).find('i').hasClass('fa-plus') ) {
	  $(this).find('i').removeClass('fa-plus').addClass('fa-minus');
	  $(this).parent().parent().find('.indent').css("display", "block");
	  $(this).parent().parent().find('.indent').removeClass('closed');
	  $('.togglechild').addClass('toggle-open');
	}
});
$('.togglechild').on('click', function(){
	if ( $(this).hasClass('toggle-open') ) {
	  $('.subheading').each(function() {
		$(this).find('i').removeClass('fa-minus').addClass('fa-plus');
		$(this).parent().find('.indent').css("display", "none");
		$(this).parent().parent().find('.indent').addClass('closed');
	  });
	  $('.togglechild').removeClass('toggle-open');
	}
	else{
	  $('.subheading').each(function() {
		$(this).find('i').removeClass('fa-plus').addClass('fa-minus');
		$(this).parent().find('.indent').css("display", "block");
		$(this).parent().parent().find('.indent').removeClass('closed');
	  });
	  $('.togglechild').addClass('toggle-open');
	}
});
});
body{
background: #fff;
margin-top:20px;
}
h1.heading {
font: 'Oswald';
text-transform: uppercase;
}
td { background: #f1f1f1; border-bottom:1px solid #ccc; border-right:1px solid #ccc; padding:20px; margin:5px; border-top:1px solid #fff; border-left:1px solid #fff; }
.wrappingmapping {
	margin:20px 0 0 20px;
	border-radius:85px;
	overflow:hidden;
	border:10px solid #fff;
	box-shadow:0 0 10px #999;
}
.menu {
margin-bottom: 50px;
}
.submenu {
padding: 20px;
background: repeating-linear-gradient(
-45deg,
#999,
#999 10px,
#888 10px,
#888 20px
);
border-radius: 2px;
}
.green {
	color:#28a745;
}
.heading {
color: #000;
background: #ccc;
border-bottom: 1px solid #ccc;
padding: 5px;
}
.subheading {
background: #fff;
padding-left: 10px;
background: #f1f1f1;
border-top: 1px solid #fff;
font-size: 30px;
}
.indent {
background: #fff;
padding: 0px 20px 20px 20px;
}
.icon {
width: 64px;
height: 64px;
}
.gallery {
width: 100%;
*width: 99.94877049180327%;
margin: 0;
padding: 0;
}
.gallery.grid li {
margin: 2px 5px;
}
.gallery.grid li {
margin: 2px 5px;
display: block;
}
.gallery.grid li:hover {
background: #ccc;
}
.gallery.grid li {
display: inline-block;
border-top: 1px solid #eee;
border-right: 1px solid #ccc;
border-bottom: 1px solid #ccc;
border-left: 1px solid #eee;
padding: 6px;
position: relative;
-moz-box-sizing: border-box;
border-radius: 3px 3px 3px 3px;
background: #fff;
}
.gallery a {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">

<div class="container-fluid">
		<div>
			<span class="toggleparent btn btn-primary btn-lg" data-selector="parent_">Toggle Parents</span>
			<span class="togglechild btn btn-success btn-lg" data-selector="child_">Toggle Children</span>
		</div>
		<hr />
		<!-- parent start -->
<div id="activities" class="menu">
	<h1 class="heading">
		<a href="#"><i class="fa fa-minus" aria-hidden="true"></i></a> Activities <span style="color:#ccc;"> [57] </span>
	</h1>
	<div id="parent_activities" class="submenu">
		<!-- child start -->
		<h4 class="subheading">
			<a href="#"><i class="fa fa-minus green" aria-hidden="true"></i></a> award-medal <span style="color:#ccc;"> [6] </span>
		</h4>
		<div id="child_award-medal" class="indent">
			<ul class="gallery grid">
				<li>
					<a href="#"><img title="military medal - 🎖️" src="https://cdn.jsdelivr.net/emojione/assets/svg/1f396.svg" class="icon" role="presentation"></a>
				</li>
			</ul>
		</div>	<!-- /indent --> 
		<!-- child start -->
		<h4 class="subheading">
			<a href="#"><i class="fa fa-minus green" aria-hidden="true"></i></a> event <span style="color:#ccc;"> [19] </span>
		</h4>
		<div id="child_event" class="indent">
			<ul class="gallery grid">
				<li>
					<a href="#"><img title="jack-o-lantern - 🎃" src="https://cdn.jsdelivr.net/emojione/assets/svg/1f383.svg" class="icon" role="presentation"></a>
				</li>
			</ul>
		</div>	<!-- /indent --> 
	</div> <!-- /submenu --> 
</div> <!-- /menu --> 
<!-- parent end -->

<!-- parent start -->
<div id="animals-nature" class="menu">
	<h1 class="heading">
		<a href="#"><i class="fa fa-minus" aria-hidden="true"></i></a> Animals & Nature <span style="color:#ccc;"> [106] </span>
	</h1>
	<div id="parent_animals-nature" class="submenu">
		<!-- child start -->
		<h4 class="subheading">
			<a href="#"><i class="fa fa-minus green" aria-hidden="true"></i></a> animal-amphibian <span style="color:#ccc;"> [1] </span>
		</h4>
		<div id="child_animal-amphibian" class="indent">
			<ul class="gallery grid">
				<li>
					<a href="#"><img title="frog face - 🐸" src="https://cdn.jsdelivr.net/emojione/assets/svg/1f438.svg" class="icon" role="presentation"></a>
				</li>
			</ul>
		</div>	<!-- /indent --> 
		<!-- child start -->
		<h4 class="subheading">
			<a href="#"><i class="fa fa-minus green" aria-hidden="true"></i></a> animal-bird <span style="color:#ccc;"> [12] </span>
		</h4>
		<div id="child_animal-bird" class="indent">
			<ul class="gallery grid">
				<li>
					<a href="#"><img title="turkey - 🦃" src="https://cdn.jsdelivr.net/emojione/assets/svg/1f983.svg" class="icon" role="presentation"></a>
				</li>
			</ul>
		</div>	<!-- /indent --> 
	</div> <!-- /submenu --> 
</div> <!-- /menu --> 
<!-- parent end -->

	</div>

因为通过.parent().find('.indent')它可以找到所有.indent,但你需要找到的元素.next.indent类不是全部,所以你应该使用.parent().next('.indent').

$(document).ready(function() {
$('.toggleparent').addClass('toggle-open');
$('.heading > a').on('click', function(event) {
event.preventDefault();
if ($(this).find('i').hasClass('fa-minus')) {
$(this).find('i').removeClass('fa-minus').addClass('fa-plus');
$(this).parent().parent().find('.submenu').css("display", "none");
$(this).parent().parent().find('.submenu').addClass('closed');
if ($('.submenu.closed').length == $('.menu').length) {
$('.toggleparent').removeClass('toggle-open');
}
} else if ($(this).find('i').hasClass('fa-plus')) {
$(this).find('i').removeClass('fa-plus').addClass('fa-minus');
$(this).parent().parent().find('.submenu').css("display", "block");
$(this).parent().parent().find('.submenu').removeClass('closed');
$('.toggleparent').addClass('toggle-open');
}
});
$('.toggleparent').on('click', function() {
if ($(this).hasClass('toggle-open')) {
$('.heading').each(function() {
$(this).find('i').removeClass('fa-minus').addClass('fa-plus');
$(this).parent().find('.submenu').css("display", "none");
$(this).parent().parent().find('.submenu').addClass('closed');
});
$('.toggleparent').removeClass('toggle-open');
} else {
$('.heading').each(function() {
$(this).find('i').removeClass('fa-plus').addClass('fa-minus');
$(this).parent().find('.submenu').css("display", "block");
$(this).parent().parent().find('.submenu').removeClass('closed');
});
$('.toggleparent').addClass('toggle-open');
}
});
});
$(document).ready(function() {
$('.togglechild').addClass('toggle-open');
$('.subheading > a').on('click', function(event) {
event.preventDefault();
if ($(this).find('i').hasClass('fa-minus')) {
$(this).find('i').removeClass('fa-minus').addClass('fa-plus');
$(this).parent().next('.indent').css("display", "none");
$(this).parent().next('.indent').addClass('closed');
if ($('.indent.closed').length == $('.menu').length) {
$('.togglechild').removeClass('toggle-open');
}
} else if ($(this).find('i').hasClass('fa-plus')) {
$(this).find('i').removeClass('fa-plus').addClass('fa-minus');
$(this).parent().next('.indent').css("display", "block");
$(this).parent().next('.indent').removeClass('closed');
$('.togglechild').addClass('toggle-open');
}
});
$('.togglechild').on('click', function() {
if ($(this).hasClass('toggle-open')) {
$('.subheading').each(function() {
$(this).find('i').removeClass('fa-minus').addClass('fa-plus');
$(this).next('.indent').css("display", "none");
$(this).next('.indent').addClass('closed');
});
$('.togglechild').removeClass('toggle-open');
} else {
$('.subheading').each(function() {
$(this).find('i').removeClass('fa-plus').addClass('fa-minus');
$(this).next('.indent').css("display", "block");
$(this).next('.indent').removeClass('closed');
});
$('.togglechild').addClass('toggle-open');
}
});
});
body {
background: #fff;
margin-top: 20px;
}
h1.heading {
font: 'Oswald';
text-transform: uppercase;
}
td {
background: #f1f1f1;
border-bottom: 1px solid #ccc;
border-right: 1px solid #ccc;
padding: 20px;
margin: 5px;
border-top: 1px solid #fff;
border-left: 1px solid #fff;
}
.wrappingmapping {
margin: 20px 0 0 20px;
border-radius: 85px;
overflow: hidden;
border: 10px solid #fff;
box-shadow: 0 0 10px #999;
}
.menu {
margin-bottom: 50px;
}
.submenu {
padding: 20px;
background: repeating-linear-gradient( -45deg, #999, #999 10px, #888 10px, #888 20px);
border-radius: 2px;
}
.green {
color: #28a745;
}
.heading {
color: #000;
background: #ccc;
border-bottom: 1px solid #ccc;
padding: 5px;
}
.subheading {
background: #fff;
padding-left: 10px;
background: #f1f1f1;
border-top: 1px solid #fff;
font-size: 30px;
}
.indent {
background: #fff;
padding: 0px 20px 20px 20px;
}
.icon {
width: 64px;
height: 64px;
}
.gallery {
width: 100%;
*width: 99.94877049180327%;
margin: 0;
padding: 0;
}
.gallery.grid li {
margin: 2px 5px;
}
.gallery.grid li {
margin: 2px 5px;
display: block;
}
.gallery.grid li:hover {
background: #ccc;
}
.gallery.grid li {
display: inline-block;
border-top: 1px solid #eee;
border-right: 1px solid #ccc;
border-bottom: 1px solid #ccc;
border-left: 1px solid #eee;
padding: 6px;
position: relative;
-moz-box-sizing: border-box;
border-radius: 3px 3px 3px 3px;
background: #fff;
}
.gallery a {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
<div class="container-fluid">
<div>
<span class="toggleparent btn btn-primary btn-lg" data-selector="parent_">Toggle Parents</span>
<span class="togglechild btn btn-success btn-lg" data-selector="child_">Toggle Children</span>
</div>
<hr />
<!-- parent start -->
<div id="activities" class="menu">
<h1 class="heading">
<a href="#"><i class="fa fa-minus" aria-hidden="true"></i></a> Activities <span style="color:#ccc;"> [57] </span>
</h1>
<div id="parent_activities" class="submenu">
<!-- child start -->
<h4 class="subheading">
<a href="#"><i class="fa fa-minus green" aria-hidden="true"></i></a> award-medal <span style="color:#ccc;"> [6] </span>
</h4>
<div id="child_award-medal" class="indent">
<ul class="gallery grid">
<li>
<a href="#"><img title="military medal - 🎖️" src="https://cdn.jsdelivr.net/emojione/assets/svg/1f396.svg" class="icon" role="presentation"></a>
</li>
</ul>
</div>
<!-- /indent -->
<!-- child start -->
<h4 class="subheading">
<a href="#"><i class="fa fa-minus green" aria-hidden="true"></i></a> event <span style="color:#ccc;"> [19] </span>
</h4>
<div id="child_event" class="indent">
<ul class="gallery grid">
<li>
<a href="#"><img title="jack-o-lantern - 🎃" src="https://cdn.jsdelivr.net/emojione/assets/svg/1f383.svg" class="icon" role="presentation"></a>
</li>
</ul>
</div>
<!-- /indent -->
</div>
<!-- /submenu -->
</div>
<!-- /menu -->
<!-- parent end -->

<!-- parent start -->
<div id="animals-nature" class="menu">
<h1 class="heading">
<a href="#"><i class="fa fa-minus" aria-hidden="true"></i></a> Animals & Nature <span style="color:#ccc;"> [106] </span>
</h1>
<div id="parent_animals-nature" class="submenu">
<!-- child start -->
<h4 class="subheading">
<a href="#"><i class="fa fa-minus green" aria-hidden="true"></i></a> animal-amphibian <span style="color:#ccc;"> [1] </span>
</h4>
<div id="child_animal-amphibian" class="indent">
<ul class="gallery grid">
<li>
<a href="#"><img title="frog face - 🐸" src="https://cdn.jsdelivr.net/emojione/assets/svg/1f438.svg" class="icon" role="presentation"></a>
</li>
</ul>
</div>
<!-- /indent -->
<!-- child start -->
<h4 class="subheading">
<a href="#"><i class="fa fa-minus green" aria-hidden="true"></i></a> animal-bird <span style="color:#ccc;"> [12] </span>
</h4>
<div id="child_animal-bird" class="indent">
<ul class="gallery grid">
<li>
<a href="#"><img title="turkey - 🦃" src="https://cdn.jsdelivr.net/emojione/assets/svg/1f983.svg" class="icon" role="presentation"></a>
</li>
</ul>
</div>
<!-- /indent -->
</div>
<!-- /submenu -->
</div>
<!-- /menu -->
<!-- parent end -->

</div>

相关内容

  • 没有找到相关文章

最新更新