我正在尝试制作一个可折叠的 div/面板/行。我想使用 jquery/javascript 添加箭头图标(右箭头和下箭头)。你知道如何在 jQuery 上翻译我的 CSS 代码吗?
这是我的 jQuery 代码:
$(document).ready(function(){
$(".collapse").on("hide.bs.collapse", function(){
$(".arrows").after().css({"content":"\e080","font-family": "Glyphicons Halflings"});
});
$(".collapse").on("show.bs.collapse", function(){
$(".arrows").after().css({"content":"\e114","font-family": "Glyphicons Halflings"});
});
});
这是我的 CSS 代码:
.arrows:after {
font-family: "Glyphicons Halflings";
content: "\e114";
}
.arrows.collapsed:after {
font-family: "Glyphicons Halflings";
content: "\e080";
}
这是我的 HTML 代码:
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#collapse1">Collapsible panel <span class="arrows"></span></a>
</h4>
</div>
<div id="collapse1" class="panel-collapse collapse">
<div class="panel-body">Panel Body</div>
<div class="panel-footer">Panel Footer</div>
</div>
</div>
</div>
最佳答案
检查这个片段。只需在隐藏和显示事件上切换类
$(".collapse").on("hide.bs.collapse", function() {
$(this).parent().find('.arrows').addClass('collapsed');
});
$(".collapse").on("show.bs.collapse", function() {
$(this).parent().find('.arrows').removeClass('collapsed');
});
.arrows:after {
font-family: "Glyphicons Halflings";
content: "\e114";
}
.arrows.collapsed:after {
font-family: "Glyphicons Halflings";
content: "\e080";
}
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#collapse1">Collapsible panel <span class="arrows collapsed"></span></a>
</h4>
</div>
<div id="collapse1" class="panel-collapse collapse">
<div class="panel-body">Panel Body</div>
<div class="panel-footer">Panel Footer</div>
</div>
</div>
</div>
</body>
</html>
关于javascript - 将 Css 转换为 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36026583/