在点击父元素时为当前子元素添加类。

huangapple go评论70阅读模式
英文:

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.

&lt;ul class=&quot;woo-main-cat&quot;&gt;

&lt;li class=&quot;cork&quot;&gt;&lt;a href=&quot;http://maincategories&quot;&gt;Anti-Allergics&lt;/a&gt;
	&lt;ul class=&quot;sub-cat-menu&quot;&gt;
	  &lt;li&gt;&lt;a href=&quot;http://maincategoreis/sub-categories)&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;
&lt;/li&gt;

&lt;li class=&quot;cork&quot;&gt;&lt;a href=&quot;http://maincategories&quot;&gt;Anti-Allergics&lt;/a&gt;
	&lt;ul class=&quot;sub-cat-menu&quot;&gt;
	  &lt;li&gt;&lt;a href=&quot;http://maincategoreis/sub-categories)&lt;/a&gt;&lt;/li&gt;
	  &lt;li&gt;&lt;a href=&quot;http://maincategoreis/sub-categories)&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;
&lt;/li&gt;

&lt;li class=&quot;cork&quot;&gt;&lt;a href=&quot;http://maincategories&quot;&gt;Anti-Allergics&lt;/a&gt;
	&lt;ul class=&quot;sub-cat-menu&quot;&gt;
	  &lt;li&gt;&lt;a href=&quot;http://maincategoreis/sub-categories)&lt;/a&gt;&lt;/li&gt;
	  &lt;li&gt;&lt;a href=&quot;http://maincategoreis/sub-categories)&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;
&lt;/li&gt;

<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

$(&#39;.woo-main-cat li&#39;).on(&#39;click&#39;, function(){
$(this).children(&#39;.sub-cat-menu&#39;).addClass(&#39;tako-open&#39;);
});

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($) {
$(&#39;.woo-main-cat li&#39;).on(&#39;click&#39;, function(){
    $(this).children(&#39;.sub-cat-menu&#39;).addClass(&#39;tako-open&#39;);
},function() {
       $(this).children(&#39;.sub-cat-menu&#39;).removeClass(&#39;tako-open&#39;);
    });
});

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”类。

$(&#39;.woo-main-cat &gt; li&#39;).on(&#39;click&#39;, function(){
  $(&#39;.sub-cat-menu&#39;).removeClass(&#39;tako-open&#39;);
  $(this).children(&#39;.sub-cat-menu&#39;).addClass(&#39;tako-open&#39;);
});
英文:

You can first remove the tako-open class from all the elements before opening the current one.

$(&#39;.woo-main-cat &gt; li&#39;).on(&#39;click&#39;, function(){
  $(&#39;.sub-cat-menu&#39;).removeClass(&#39;tako-open&#39;);
  $(this).children(&#39;.sub-cat-menu&#39;).addClass(&#39;tako-open&#39;);
});

答案2

得分: 0

$(&#39;.woo-main-cat li&#39;).on(&#39;click&#39;, function(){
$( &quot;*&quot; ).removeClass( &quot;tako-open&quot; );
$(this).children(&#39;.sub-cat-menu&#39;).addClass(&#39;tako-open&#39;,1000, &quot;easeOutBounce&quot;);
})
英文:
$(&#39;.woo-main-cat li&#39;).on(&#39;click&#39;, function(){
$( &quot;*&quot; ).removeClass( &quot;tako-open&quot; );
$(this).children(&#39;.sub-cat-menu&#39;).addClass(&#39;tako-open&#39;,1000, &quot;easeOutBounce&quot;);
    })
  • will remove all tako-open classes. ppreciate everyone's help

huangapple
  • 本文由 发表于 2023年7月20日 20:16:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定