2
經過幾個小時的試用和錯誤,我意識到我應該真的瞭解jQuery的基礎知識。有人可以幫我解答一個簡單的問題嗎?如何讓手風琴活躍的項目可點擊
我正在將this手風琴放到一個頁面中,但我希望活動面板能夠點擊關閉。有什麼簡單的方法可以做到這一點嗎?
(function($) {
//Hide all panels
var allPanels = $('.accordion > dd').hide();
//Show first panel
$('.accordion > dd:first-of-type').show();
//Add active class to first panel
$('.accordion > dt:first-of-type').addClass('accordion-active');
//Handle click function
jQuery('.accordion > dt').on('click', function() {
//this clicked panel
$this = $(this);
//the target panel content
$target = $this.next();
//Only toggle non-displayed
if(!$this.hasClass('accordion-active')){
//slide up any open panels and remove active class
$this.parent().children('dd').slideUp();
//remove any active class
jQuery('.accordion > dt').removeClass('accordion-active');
//add active class
$this.addClass('accordion-active');
//slide down target panel
$target.addClass('active').slideDown();
}
return false;
});
})(jQuery);
也許我錯了,但是......不是'.parent()。children('dd')'只是一個更長的說法'.siblings('dd')'?我不經常使用它,所以我其實很好奇 – Deryck
Brillant。感謝PSL和Deryck。像魅力一樣工作! – dotwongdotcom
@dotwongdotcom不客氣,看看第二個版本...... – PSL