简体   繁体   English

单击 JavaScript 添加和删除类功能

[英]JavaScript add and remove class function on click

Basically I have this problem.基本上我有这个问题。 I have an accordion that toggles one heading open at a time and I am having a problem adding in the open/close image that sits to the right of the heading.我有一个手风琴,每次打开一个标题,我在添加位于标题右侧的打开/关闭图像时遇到问题。

I have it so far so that once you click a heading it removes the 'open' image and toggles another class for the 'close' image.到目前为止,我已经有了它,因此一旦您单击标题,它就会删除“打开”图像并为“关闭”图像切换另一个类。 Now I need to basically swap out these classes again so that if you toggle another heading it removes the other image and goes back to the original.现在我需要再次更换这些类,以便如果您切换另一个标题,它会删除另一个图像并返回到原始图像。

Here is the code I am using.这是我正在使用的代码。

JavaScript JavaScript

<SCRIPT>
    $("#accordion > li").click(function () {
        $("#accordian li").removeClass("faq-header");
        $(this).addClass("faq-header2");
        if (false == $(this).next().is(':visible')) {
            $('#accordion > ul').slideUp(250);
            $('#accordion > ul').addClass('faq-header');
            $(this).removeClass("faq-header");
        }
        $(this).next().slideToggle(300);
    });

    $('#accordion > ul:eq(0)').show();
</SCRIPT>

CSS CSS

#accordion {
    list-style: none;
    margin-left:-38px;
}
#accordion ul:eq {
    background-image:url(../img/faq-open.gif);
    background-repeat:no-repeat;
    background-position:right;
    padding-right:20px;
}
#accordion li{
    display: block;
    background-color: #FFF;
    font-weight: bold;
    cursor: pointer;
}
.faq-header {
    text-align:left;
    background-image:url(../img/faq-close.gif);
    background-repeat:no-repeat;
    background-position:right;
    margin-right:20px;
}
.faq-header2 {
    text-align:left;
    background-image:url(../img/faq-open.gif);
    background-repeat:no-repeat;
    background-position:right;
    margin-right:20px;
}
#accordion ul {
    list-style: none;
    display: none;
}
#accordion ul li{
    font-weight: normal;
    cursor: auto;
    background-color: #fff;
    border-bottom:1px solid #999;
    margin-left:-38px !important;
}

I have removed one class and added another class as you can see $("#accordian li").removeClass("faq-header");我删除了一个类并添加了另一个类,如您所见$("#accordian li").removeClass("faq-header"); and added the following $(this).addClass("faq-header2");并添加了以下$(this).addClass("faq-header2");

But I need to now remove .faq-header2 and add back .faq-header after it is no longer the section selected.但是我现在需要删除.faq-header2并在它不再是所选部分后添加回.faq-header It doesn't seem too hard to me but i just can't figure out how to code it.这对我来说似乎并不难,但我就是不知道如何编码。 Should be a basic if function I would think...我认为应该是一个基本的 if 函数...

The jQuery UI accordion is a well proven cross browser widget, and does images on open and close (by default on the left, but one change of CSS will put them on the right) jQuery UI 手风琴是一个经过充分验证的跨浏览器小部件,并在打开和关闭时显示图像(默认情况下在左侧,但更改 CSS 会将它们放在右侧)

as per here按照这里

if you don't won't to use it, I would persue an option with toggleClass, here如果您不想使用它,我会在这里使用toggleClass 提出一个选项

EDIT编辑

Thanks for posting your HTML, I didn't necessarily mean the whole page, just the HTML for you accordion functionality, but hey thats cool感谢您发布您的 HTML,我不一定是指整个页面,只是为您提供手风琴功能的 HTML,但是嘿,这很酷

First point though, your HTML seems a bit heavy for just doing an accordion.不过,第一点,你的 HTML 似乎只是做手风琴有点沉重。 Its also not entirely valid to put a ul inside a ul (they tend to go inside li, as in a drop down menu style).将 ul 放入 ul 中也不完全有效(它们倾向于放入 li 中,就像在下拉菜单样式中一样)。 Further more it doesn't seem to be much point in all those ul and li as each ul only has one li anyway, just seems like a lot more tags than you would really need.此外,所有这些 ul 和 li 似乎都没有多大意义,因为无论如何每个 ul 都只有一个 li,看起来标签比您真正需要的要多得多。 ul and li tend to come with a lot of default styling (bullet points, margins, padding, indents etc), which can mean a lot more CSS than need to make them display how you want. ul 和 li 往往带有很多默认样式(项目符号、边距、内边距、缩进等),这意味着需要更多的 CSS 来让它们以您想要的方式显示。 I would have gone with a simpler structure, makes it easier to write your jQuery.我会采用更简单的结构,这样可以更轻松地编写 jQuery。 A bit more like this有点像这样

<div id="accordion">
    <h3>First header</h3>
    <div>First content</div>
    <h3>Second header</h3>
    <div>Second content</div>
</div>

Anyway, that was just a comment from my experience.无论如何,这只是我的经验的评论。 To your problem at hand, this worked for me对于您手头的问题,这对我有用

    $("#accordion > li").click(function () {              
            var self = $(this);
            self.next('ul').slideToggle(300, function () {
                if ($(this).is(':visible')) {
                    self.removeClass('faq-header').addClass('faq-header2')
                }
                else {
                    self.removeClass('faq-header2').addClass('faq-header')
                }
            });
            self.siblings().removeClass('faq-header2').addClass('faq-header').next('ul').slideUp(250);
        });

toggleClass, although useful, in your circumstance as to how you want classes to be added and removed, may not be as useful as I would have thought toggleClass 虽然很有用,但在您希望如何添加和删除类的情况下,可能没有我想象的那么有用

I think that toggle() function is a bit not intuitive.我认为toggle()函数有点不直观。 I usually use my pwn method replaceClass() to do as you want:我通常使用我的 pwn 方法 replaceClass() 来做你想做的:

void replaceClass(Object classList, String surrogateClass, String classToReplace);

defined as follow定义如下

function replaceClass(elementClassList, firstClass, secondClass) {
 if(elementClassList.contains(firstClass)){
 {
  elementClassList.remove(firstClass);
elementClassList.add(secondClass);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM