除了我不熟悉JavaScript外,还有几天没有找到答案的问题。
$(document).on('click', '.content-click', function(){
$(".content-click span").toggleClass("clicked"),
$(".content-view").toggleClass("viewed");
$(this).show();
});
.content-click {
display: block;
width: 100%;
height: 2rem;
padding: 0.375rem 0.75rem;
font-size: .75rem;
font-weight: 500;
line-height: 1.5;
color: #495057;
background-color: #fff;
background-clip: padding-box;
border: 1px solid #ced4da;
border-radius: 0.25rem;
cursor: pointer;
}
.content-click p {
display: inline;
margin: 0;
}
.content-click span {
width: 10px;
height: 10px;
position: relative;
top: 5px;
float: right;
vertical-align: middle;
background: url(../img/arrow.png) no-repeat;
transition: all 0.3s ease;
transform: rotate(0deg);
}
.content-click span.clicked {
transform: rotate(90deg);
} /*button click styling */
.content-view {
display: block;
width: 100%;
height: 0;
border: 0px solid #ebebeb;
box-sizing: border-box;
margin-top: 0rem;
margin-bottom: 0rem;
position: relative;
border-radius: .25rem;
padding: 0;
font-size: 0;
font-weight: 500;
opacity: 0;
transition: all 0.2s ease;
}
.content-view::after {
content: '';
width: 10px;
height: 10px;
border-top: 1px solid #ebebeb;
border-right: 0px solid #ebebeb;
border-bottom: 0px solid #ebebeb;
border-left: 1px solid #ebebeb;
position: absolute;
left: 95%;
top: 0%;
margin-top: -6px;
margin-left: -6px;
transform: rotate(45deg);
background-color: #fff;
}
.content-view.viewed {
height: auto;
border: 1px solid #ebebeb;
margin-top: .25rem;
margin-bottom: 1rem;
font-size: .75rem;
padding: 1rem;
opacity: 1;
} /*description area-text styling*/
<div class="container">
<div class="content-click" style="margin:.25rem;">
<div id="content-1">
<p>First Item list...</p>
<span></span>
</div>
</div>
<div class="content-view">
<div id="view-1">
<p>Description...</p>
</div>
</div>
<div class="content-click" style="margin:.25rem;">
<div id="content-2">
<p>First Item list...</p>
<span></span>
</div>
</div>
<div class="content-view">
<div id="view-2">
<p>Description...</p>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
将类名添加为.toggleClass目标后,具有完全相同类的相同项将弹出描述,即使我仅单击第一个按钮也是如此。很抱歉提出愚蠢的问题。
答案 0 :(得分:0)
您需要维护click事件的范围,以便每当您单击一个项目时,只会展开下一个说明,而不会全部展开。
代码如下:
$(document).on('click', '.content-click', function(){
$(this).find("span").toggleClass("clicked"); // this finds the child element 'span'
$(this).next().toggleClass("viewed"); // .next() is the selector for the next sibling element;
$(this).show();
});
.content-click {
display: block;
width: 100%;
height: 2rem;
padding: 0.375rem 0.75rem;
font-size: .75rem;
font-weight: 500;
line-height: 1.5;
color: #495057;
background-color: #fff;
background-clip: padding-box;
border: 1px solid #ced4da;
border-radius: 0.25rem;
cursor: pointer;
}
.content-click p {
display: inline;
margin: 0;
}
.content-click span {
width: 10px;
height: 10px;
position: relative;
top: 5px;
float: right;
vertical-align: middle;
background: url(../img/arrow.png) no-repeat;
transition: all 0.3s ease;
transform: rotate(0deg);
}
.content-click span.clicked {
transform: rotate(90deg);
} /*button click styling */
.content-view {
display: block;
width: 100%;
height: 0;
border: 0px solid #ebebeb;
box-sizing: border-box;
margin-top: 0rem;
margin-bottom: 0rem;
position: relative;
border-radius: .25rem;
padding: 0;
font-size: 0;
font-weight: 500;
opacity: 0;
transition: all 0.2s ease;
}
.content-view::after {
content: '';
width: 10px;
height: 10px;
border-top: 1px solid #ebebeb;
border-right: 0px solid #ebebeb;
border-bottom: 0px solid #ebebeb;
border-left: 1px solid #ebebeb;
position: absolute;
left: 95%;
top: 0%;
margin-top: -6px;
margin-left: -6px;
transform: rotate(45deg);
background-color: #fff;
}
.content-view.viewed {
height: auto;
border: 1px solid #ebebeb;
margin-top: .25rem;
margin-bottom: 1rem;
font-size: .75rem;
padding: 1rem;
opacity: 1;
} /*description area-text styling*/
<div class="container">
<div class="content-click" style="margin:.25rem;">
<div id="content-1">
<p>First Item list...</p>
<span></span>
</div>
</div>
<div class="content-view">
<div id="view-1">
<p>Description...</p>
</div>
</div>
<div class="content-click" style="margin:.25rem;">
<div id="content-2">
<p>First Item list...</p>
<span></span>
</div>
</div>
<div class="content-view">
<div id="view-2">
<p>Description...</p>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
答案 1 :(得分:0)