活动类兄弟姐妹removeClass [英] Active class siblings removeClass

查看:92
本文介绍了活动类兄弟姐妹removeClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有各种类别为.folder

当我单击一个类时,我想向其添加类active,并从任何其他元素中删除该类.

When I click one I want to add the class active to it and remove the class from any other element.

问题是首先将类从其他类中删除,根本就是不这样做.

The problem is removing the class from the others first, its simply not doing it.

这就是我要尝试的:

$('.folder').click(function() {
    $(this).siblings('.folder').removeClass('active'); // remove from all other <SPAN>s
    $(this).addClass('active'); // add onto current
})

HTML示例:

<ul>
    <li class="closed last">
        <span class="folder" id="16">More coding</span>
    </li>
</ul>  
</li>
<li class="closed collapsable lastCollapsable">
    <span class="folder active" id="12">Music</span>
    <ul style="display: block;" id="browser" class="filetree treeview-famfamfam">
        <li class="closed collapsable">
            <span class="folder active" id="13">Music child</span>
                <ul style="display: block;" id="browser" class="filetree treeview-famfamfam">
                    <li class="closed last">
                        <span class="folder" id="15">More music</span>
                    </li>
                </ul>
         </li>
         <li class="closed last"><span class="folder" id="17">Another child here</span> </li>
     </ul>
    </li>
</ul>

推荐答案

根据我的观点,从所有元素中删除类

As per my opinion, remove class from all eleemnts

$('.folder').click(function() {
    $('.folder.active').removeClass('active'); // Just remove class from all folder
    $(this).addClass('active'); // add onto current
})

这篇关于活动类兄弟姐妹removeClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆