英文:
Adding class to current child when click on parent element
问题
我有一个电商网站,尝试在点击父菜单时打开子菜单。在类别侧边栏中,当单击父元素(主类别)时,我想给子元素(子类别)添加类,以便以一些漂亮的动画显示子元素。
当单击下一个主类别时,先前的子类别应该被隐藏,当前的子类别应该显示。
<ul class="woo-main-cat">
<li class="cork"><a href="http://maincategories">抗过敏药物</a>
<ul class="sub-cat-menu">
<li><a href="http://maincategories/sub-categories">子类别</a></li>
</ul>
</li>
<li class="cork"><a href="http://maincategories">抗过敏药物</a>
<ul class="sub-cat-menu">
<li><a href="http://maincategories/sub-categories">子类别</a></li>
<li><a href="http://maincategories/sub-categories">子类别</a></li>
</ul>
</li>
<li class="cork"><a href="http://maincategories">抗过敏药物</a>
<ul class="sub-cat-menu">
<li><a href="http://maincategories/sub-categories">子类别</a></li>
<li><a href="http://maincategories/sub-categories">子类别</a></li>
</ul>
</li>
</ul>
其中类:woo-main-cat
> li
包含主类别,类:sub-cat-menu
> li
包含子类别。当单击 woo-main-cat
> li
时,子类别菜单应该显示,当单击下一个主类别时,先前的子类别应该关闭。
.sub-cat-menu {
background: #fff;
visibility: hidden;
height: 0;
transition: 0.8s ease-in;
}
.tako-open {
visibility: visible;
height: 100%;
}
尝试一些 CSS 动画效果,当子类别打开和隐藏时。
$('.woo-main-cat li').on('click', function(){
$(this).children('.sub-cat-menu').addClass('tako-open');
});
上述代码完美地在单击父类别时打开当前子类别,向子元素的子类别菜单添加 tako-open
类,但我希望在单击下一个父类别时关闭此打开的子类别。
jQuery(function($) {
$('.woo-main-cat li').on('click', function(){
$(this).children('.sub-cat-menu').addClass('tako-open');
},function() {
$(this).children('.sub-cat-menu').removeClass('tako-open');
});
});
所以在添加了 removeClass
代码之后什么也没发生,当单击父元素(主类别)时它不会给子元素(子类别)添加任何类。
英文:
i have ecommerce site, trying open sub-menu when click on parent menu
In category side-bar when parent element (main category) is clicked i want add class to child element (sub-category) so that child element is display with some nice animation.
categories menu
when next main category is click previous sub-category should be hidden and current sub-category should be displayed.
<ul class="woo-main-cat">
<li class="cork"><a href="http://maincategories">Anti-Allergics</a>
<ul class="sub-cat-menu">
<li><a href="http://maincategoreis/sub-categories)</a></li>
</ul>
</li>
<li class="cork"><a href="http://maincategories">Anti-Allergics</a>
<ul class="sub-cat-menu">
<li><a href="http://maincategoreis/sub-categories)</a></li>
<li><a href="http://maincategoreis/sub-categories)</a></li>
</ul>
</li>
<li class="cork"><a href="http://maincategories">Anti-Allergics</a>
<ul class="sub-cat-menu">
<li><a href="http://maincategoreis/sub-categories)</a></li>
<li><a href="http://maincategoreis/sub-categories)</a></li>
</ul>
</li>
<ul>
Where class:woo-main-cat > li holds main categories and class:sub-cat-menu > li holds sub-categories. When woo-main-cat > li is click sub-cat-menu should displayed when next main categories is clicked previous sub-category should be close.
.sub-cat-menu {
background: #fff;
visibility: hidden;
height: 0;
transition: 0.8s ease-in;
}
.tako-open{
visibility: visible;
height: 100%;
}
Trying some animation effect with css when sub-categories opens and hide
$('.woo-main-cat li').on('click', function(){
$(this).children('.sub-cat-menu').addClass('tako-open');
});
Above code perfectly open the current sub-category adding tako-open class on child element->sub-cat-menu when click on parent category but i want close this open sub-category when next parent category is click.
jQuery(function($) {
$('.woo-main-cat li').on('click', function(){
$(this).children('.sub-cat-menu').addClass('tako-open');
},function() {
$(this).children('.sub-cat-menu').removeClass('tako-open');
});
});
so after adding removeClass code nothing happens it doesnt add any class to child-element (sub-category) when click on parent element (main-category)
答案1
得分: 0
你可以首先在打开当前元素之前从所有元素中移除“tako-open”类。
$('.woo-main-cat > li').on('click', function(){
$('.sub-cat-menu').removeClass('tako-open');
$(this).children('.sub-cat-menu').addClass('tako-open');
});
英文:
You can first remove the tako-open
class from all the elements before opening the current one.
$('.woo-main-cat > li').on('click', function(){
$('.sub-cat-menu').removeClass('tako-open');
$(this).children('.sub-cat-menu').addClass('tako-open');
});
答案2
得分: 0
$('.woo-main-cat li').on('click', function(){
$( "*" ).removeClass( "tako-open" );
$(this).children('.sub-cat-menu').addClass('tako-open',1000, "easeOutBounce");
})
英文:
$('.woo-main-cat li').on('click', function(){
$( "*" ).removeClass( "tako-open" );
$(this).children('.sub-cat-menu').addClass('tako-open',1000, "easeOutBounce");
})
- will remove all tako-open classes. ppreciate everyone's help
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论