Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery.removeClass问题_Jquery_Css - Fatal编程技术网

jQuery.removeClass问题

jQuery.removeClass问题,jquery,css,Jquery,Css,我在我的网站上使用了一个词汇表,它利用了一些显示/隐藏功能和.removeClass属性 $(document).ready(function() { $("#glossary-container li").click(function() { $(".glossary-content > div").hide(); var id = $(this).attr("id").replace('-link', ''); $('#' +

我在我的网站上使用了一个词汇表,它利用了一些显示/隐藏功能和.removeClass属性

$(document).ready(function() {
    $("#glossary-container li").click(function() { 
        $(".glossary-content > div").hide();
        var id = $(this).attr("id").replace('-link', ''); 
        $('#' + id).show(); 

        $(".glossaryUL li").each(function() { 
            $(this).click(function() {
                $(".glossaryUL li").removeClass("selected");                       
                $(this).addClass("selected"); 
                elementClick = $(this).attr("id"); 
            });
        });
    }); 

    $('.coolbox').hide(); 

    $('.clicker').click(function(event){ 
        event.preventDefault(); 
        $($(this).attr('href')).toggle(300); 
    });
});
在页面加载时,列表项字母“A”the UL lass.glossaryUL“通过内联样式设置为“selected”

  • a
  • 当您单击另一个字母时,将显示class“.glossary content”中的术语,但.removeClass属性不会从字母“A”中删除该类

    但是,如果随后单击另一个字母(页面加载后第二次单击),.removeClass属性将运行并删除该类,并将其应用于正确的字母

    我不明白它为什么这样做


    非常感谢您提供的任何帮助。

    您不需要将单击事件分配包装到
    每个
    ,jQuery已经为与该选择器匹配的每个元素分配了
    单击
    事件(因为您使用了类作为选择器)。我怀疑你有重复的点击事件。请尝试以下方法:

    $(".glossaryUL li").click(function() { 
            $(".glossaryUL li").removeClass("selected");                       
            $(this).addClass("selected"); 
            elementClick = $(this).attr("id"); 
        });
    });
    

    您能提供一个JSFIDLE来演示这种情况吗?您的jQuery代码似乎有点不稳定。你可以发布你拥有的HTML,因为这可能需要大约5行代码。嗨,没有时间回答你,但是请检查搜索(…on(event){event.prevent…),因为event是一个保留词。也许我完全错了,不想引入错误的方向,只需指出我的一些错误的纪念品。
    $(".glossaryUL li").click(function() { 
            $(".glossaryUL li").removeClass("selected");                       
            $(this).addClass("selected"); 
            elementClick = $(this).attr("id"); 
        });
    });