HTML
<div id="accordion">
<h1 class="name">Cat<i class="fa fa-chevron-down"></i></h1>
<div class="accordion-content">
content content 1
</div>
<h1 class="name">Cow<i class="fa fa-chevron-down"></i></h1>
<div class="accordion-content">
content content 2
</div>
</div>
JQuery的
$('#accordion h1').on('click', function(event){
event.stopPropagation();
event.preventDefault();
var openMenu = $('#accordion .active').next('.accordion-content');
if(event.handled !== true) {
openMenu.velocity("slideUp", function() {});
if ($(this).hasClass('active')){
$(this).removeClass('active');
$('i',this).removeClass('fa-chevron-up');
$('i',this).next().addClass('fa-chevron-down');
} else{
$(this).next('.accordion-content').velocity("slideDown", function() {});
$('#accordion h1').removeClass('active');
$(this).addClass('active');
console.log('no active but added active');
console.log($('i', this) + "here i");
$('i',this).removeClass('fa-chevron-down');
$('i',this).not('fa-chevron-up').addClass('fa-chevron-up');
}
event.handled = true;
} else {
return false;
}
});
如小提琴,當點擊H1一次又一次兩次,箭頭:上下切換正確。
但是當點擊另一個h1時,然後「打開」的手風琴內容將關閉並打開另一個手風琴內容。所以箭頭應該相應地切換。
當點擊另一個h1時,其手風琴內容將打開,h1將顯示向上箭頭。
當再次點擊另一個不同的h1時,先前打開的內容將關閉,h1將顯示向下箭頭,並且新內容將在h1上向上箭頭打開。
偶試過
$('i', this).toggleClass('fa-chevron-down fa-chevron-up');
但它不工作要麼。
如何根據開/關手風琴內容改變箭頭?
5秒修復。 http://jsfiddle.net/v9cnLky0/2/ - 因爲我沒有足夠徹底地檢查,所以沒有發帖作爲回答。 – techfoobar 2015-04-01 05:32:23
查看我的回答 – 2015-04-01 05:38:24