2016-03-16 98 views
0

我正在嘗試製作可摺疊的div /面板/行。我想用jquery/javascript添加箭頭圖標(向右和向下箭頭)。你有關於如何在jQuery上翻譯我的CSS代碼的想法嗎?將CSS轉換爲jQuery

這裏是我的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> 
+0

只需添加/刪除類,而不是通過JS – Tushar

+0

應用CSS我想改變我的跨度使用jQuery/JavaScript的(.arrows)的內容。 – darkvirus24

+0

可能的重複http://stackoverflow.com/questions/4481485/changing-css-pseudo-element-styles-via-javascript –

回答

0

檢查這個snipped.just切換上隱藏的類並顯示事件

$(".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>