jQuery accordion remove siblings class
I am able to change the CSS when I clicked on a title to expand a menu. However other 'header' which has been expanded are unable to change to "inactive" class. Below is my HTML
<dt class="productsCat"><a href="/" class="productsName">Category 1</a></dt>
<dd class="subCat"><a href="<?php echo $subCategory->getUrl()?>">Sub Category 1</a></dd>
<dt class="productsCat"><a href="/" class="productsName">Category 2</a></dt>
<dd class="subCat"><a href="<?php echo $subCategory->getUrl()?>">Sub Category 2</a></dd>
And here is my jQuery
jQuery(document).ready(function(){
jQuery("dl#narrow-by-list> dd:not(:first)").hide();
jQuery("dl#narrow-by-list> dt a").click(function(){
jQuery("dl#narrow-by-list> dd:visible").slideUp("fast");
jQuery(this).parent().next().slideDown("fast");
jQuery(this).parent().removeClass("productsCat");
jQuery(this).parent().addClass("openSub");
return false;
});
});
Any advice how I should remove the class "openSub" and use the existing class "productsCat" from all the other siblings when one is act开发者_高级运维ive?
Thank you!
So, try this, works in my local..
$(function() {
$("dl#narrow-by-list> dd:not(:first)").hide();
$("dl#narrow-by-list > dt").first().addClass('openSub');
$(".productsName").click(function () {
if(!$(this).closest('dt').hasClass('openSub')) {
$("dl#narrow-by-list> dd:visible").slideUp("fast");
}
$(this).parent().next().slideDown("fast");
$(this).parent().each(function () {
$(this).parents('dl').find('dt').each(function() {
$(this).addClass('productsCat');
$(this).removeClass('openSub');
});
$(this).closest('dt').toggleClass('productsCat openSub');
});
return false;
});
});
Not shure what you want, but maybe something like this? Else you could try the toggleclass?
jQuery(document).ready(function () {
jQuery("dl#narrow-by-list> dd:not(:first)").hide();
jQuery("dl#narrow-by-list> dt a").click(function () {
jQuery("dl#narrow-by-list> dd:visible").slideUp("fast");
jQuery(this).parent().next().slideDown("fast");
jQuery(this).parent().each(function () {
if (jQuery(this).hasClass('productsCat')) {
jQuery(this).removeClass('productsCat');
jQuery(this).addClass("openSub");
}
});
return false;
});
});
精彩评论