我开始学习jQuery。我有一个简单的按钮,我执行slideToggle来显示/隐藏div。我在div上方有一个
下面是我的代码以及一个简单的jsfiddle示例。
https://jsfiddle.net/ewsrq961/1/
谢谢。
HTML:
<hr id="hrItem">
<div id="actionItems">
<h2>Hello There!</h2>
</div>
<div id="actionsBtn">
<a id="btnAction" href="javascript:void(0)" rel="tooltip" title="Open Actions" class="btn faa-vertical animated-hover" data-original-title="Open Actions"><i class="fa fa-chevron-circle-down" aria-hidden="true" style="padding-right: 10px;"></i>Actions</a>
</div>
CSS:
#actionItems {
display: none;
padding-bottom: 15px;
}
#actionsBtn {
text-align: center;
background-color: #252d44;
width: 120px;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
margin: 0 auto;
margin-top: 0px;
margin-bottom: 0px;
margin-top: 0;
margin-bottom: 20px;
color: #fff !important;
padding:5px;
}
#actionsBtn a {
color: #fff;
}
JS:
// Hide/Show div
$("#btnAction").click(function(){
$("#actionItems").slideToggle();
$(this).find('i').toggleClass('fa-chevron-circle-down fa-chevron-circle-up')
});
答案 0 :(得分:2)
虽然您已将#hrItem
标识为hr
,但使用display : none;
为其添加css样式非常简单,然后将#hrItem
添加到slideToggle
的类中js代码
// Hide/Show Actions Panel
$("#btnAction").click(function(){
$("#actionItems , #hrItem").slideToggle(); //<<<< add hr id
$(this).find('i').toggleClass('fa-chevron-circle-down fa-chevron-circle-up')
});
&#13;
#hrItem{ /* style hr*/
display : none;
}
#actionItems {
display: none;
padding-bottom: 15px;
}
#actionsBtn {
text-align: center;
background-color: #252d44;
width: 120px;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
margin: 0 auto;
margin-top: 0px;
margin-bottom: 0px;
margin-top: 0;
margin-bottom: 20px;
color: #fff !important;
padding:5px;
}
#actionsBtn a {
color: #fff;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<hr id="hrItem">
<div id="actionItems">
<h2>Hello There!</h2>
</div>
<div id="actionsBtn">
<a id="btnAction" href="javascript:void(0)" rel="tooltip" title="Open Actions" class="btn faa-vertical animated-hover" data-original-title="Open Actions">
<i class="fa fa-chevron-circle-down" aria-hidden="true" style="padding-right: 10px;"></i>
Actions
</a>
</div>
&#13;
答案 1 :(得分:1)
我认为你想在div关闭时显示它,并在div选项时隐藏,然后你可以在点击时调用.toggle()
功能,它会在第一次点击时隐藏并在每次点击时显示。
$('#hrItem').toggle();
示例:强>
// Hide/Show Actions Panel
$("#btnAction").click(function(){
$("#actionItems").slideToggle();
$('#hrItem').toggle(); //ADDED
$(this).find('i').toggleClass('fa-chevron-circle-down fa-chevron-circle-up')
});
&#13;
#actionItems {
display: none;
padding-bottom: 15px;
}
#actionsBtn {
text-align: center;
background-color: #252d44;
width: 120px;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
margin: 0 auto;
margin-top: 0px;
margin-bottom: 0px;
margin-top: 0;
margin-bottom: 20px;
color: #fff !important;
padding:5px;
}
#actionsBtn a {
color: #fff;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<hr id="hrItem">
<div id="actionItems">
<h2>Hello There!</h2>
</div>
<div id="actionsBtn">
<a id="btnAction" href="javascript:void(0)" rel="tooltip" title="Open Actions" class="btn faa-vertical animated-hover" data-original-title="Open Actions"><i class="fa fa-chevron-circle-down" aria-hidden="true" style="padding-right: 10px;"></i>Actions</a>
</div>
&#13;
答案 2 :(得分:1)
您可以在点击事件中添加条件,根据可见性调整<hr/>
:
// Hide/Show Actions Panel
$("#btnAction").click(function() {
$('#hrItem').is(':visible') ? $('#hrItem').hide() : $('#hrItem').show();
$("#actionItems").slideToggle();
$(this).find('i').toggleClass('fa-chevron-circle-down fa-chevron-circle-up');
});
#actionItems {
display: none;
padding-bottom: 15px;
}
#actionsBtn {
text-align: center;
background-color: #252d44;
width: 120px;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
margin: 0 auto;
margin-top: 0px;
margin-bottom: 0px;
margin-top: 0;
margin-bottom: 20px;
color: #fff !important;
padding: 5px;
}
#actionsBtn a {
color: #fff;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<hr id="hrItem">
<div id="actionItems">
<h2>Hello There!</h2>
</div>
<div id="actionsBtn">
<a id="btnAction" href="javascript:void(0)" rel="tooltip" title="Open Actions" class="btn faa-vertical animated-hover" data-original-title="Open Actions"><i class="fa fa-chevron-circle-down" aria-hidden="true" style="padding-right: 10px;"></i>Actions</a>
</div>