【发布时间】:2013-12-15 23:50:35
【问题描述】:
在过去几个小时的反复试验之后,我意识到我真的应该学习 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);
【问题讨论】:
标签: javascript jquery wordpress accordion