removeclass不起作用 [英] removeclass doesnt work

查看:2125
本文介绍了removeclass不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的问题,实际上这不是问题,这正在解决javascript错误,无论如何,这是代码:

i have a very simple question, actually this is not the question, this is fixing the javascript mistake, anyway, here is the code:

    $(document).ready(function() {
        $('#accordion a.item').click(function () {
            $(this).addClass('selected');
            $(this).removeClass('selected');
            $('#accordion li').children('ul').slideUp('slow'); 
            $(this).siblings('ul').slideDown('slow');  
        });
    });

和html:

<ul id="accordion">
    <li>
        <a href="#" class="item">BANKS</a>
        <ul>BLA BLA BLA</ul>
    </li>
    <li>
        <a href="#" class="item">PETROL</a>
        <ul>BLA BLA BLA</ul>
    </li>
</ul>

当我单击链接中的一个时,已向其中添加了所选"类,但是当我单击另一个链接时,所选"类未移除,那可能是什么错误?

when i click one of the link, the 'selected' class is added to it, but when i click the other link, the class 'selected' is not removing, what can be the mistake there?

谢谢大家的帮助!我真的很感激!

thank you all for helping! i really appreciate it!

推荐答案

目前尚不清楚您要做什么,但是您似乎想将类添加到clicked元素中并删除来自所有兄弟姐妹.您当前的代码将类添加和删除到相同的元素,结果最终什么都不做.

It's not clear what you are trying to do, but it looks like you want to add the class to the clicked element and remove it from the all its siblings. Your current code adds and removes the class to the same element, which ends up not doing anything.

您可能想要类似

$(document).ready(function() {
    $items = $('#accordion a.item'); // all the items
    $items.click(function () {
        $items.removeClass('selected'); // remove class from everything
        $(this).addClass('selected'); // add class to clicked item
        $('#accordion li').children('ul').slideUp('slow'); 
        $(this).siblings('ul').slideDown('slow');  
    });
});

这篇关于removeclass不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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