简体   繁体   English

如何使用jQuery将类添加到手风琴的活动选项卡中?

[英]How do I add a class to the active tab in an accordion using jQuery?

I made this jfiddle with my code: http://jsfiddle.net/yp9v5/1/ 我用我的代码制作了这个jfiddle: http : //jsfiddle.net/yp9v5/1/

I'm trying to add a class to the open tab (class is set to the first tab which is open on page load and then switched to another tab when it is clicked). 我试图将一个类添加到“打开”选项卡(该类设置为第一个在页面加载时打开的选项卡,然后在单击该选项卡时切换到另一个选项卡)。 I want to be able to style the active tab differently. 我希望能够为活动标签设置不同的样式。

I have tried using addClass but I don't think i'm using it correctly to add the class to the active label. 我尝试使用addClass,但我认为我没有正确使用它来将类添加到活动标签。

Here is the jQuery I used: 这是我使用的jQuery:

    jQuery(".alt_block_wrapper .ac-container div label").click(function() {

    jQuery(".alt_block_wrapper .ac-container div article").css("display", "none");
    jQuery(this).parent().find("article").fadeIn("slow");
    jQuery(this).addClass("ac-active");
});
jQuery(".alt_block_wrapper .ac-container div article").css("display", "none");
jQuery(".alt_block_wrapper .ac-container div article").first().css("display", "block");

and the html: 和html:

<div class="alt_block_wrapper one-third">
        <section class="ac-container">
            <div>
                <input id="ac-1412" name="accordion-1412" type="radio" checked />
                <label for="ac-1412">First Tab</label>
                <article class="ac-medium">
                    <p>Nostraaptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p>
                </article>
            </div>
            <div>
                <input id="ac-2412" name="accordion-1412" type="radio" />
                <label for="ac-2412">Second Tab</label>
                <article class="ac-medium">
                    <p>Nostraaptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p>
                </article>
            </div>
            <div>
                <input id="ac-3412" name="accordion-1412" type="radio" />
                <label for="ac-3412">Third Tab</label>
                <article class="ac-medium">
                    <p>Nostraaptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p>
                </article>
            </div>
            <div>
                <input id="ac-4412" name="accordion-1412" type="radio" />
                <label for="ac-4412">Fourth Tab</label>
                <article class="ac-medium">
                    <p>Nostraaptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p>
                </article>
            </div>
            <div>
                <input id="ac-5412" name="accordion-1412" type="radio" />
                <label for="ac-5412">Fifth Tab</label>
                <article class="ac-medium">
                    <p>Nostraaptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p>
                </article>
            </div>
        </section>

Your issue is mainly due to Css specifity. 您的问题主要是由于CSS的特殊性。 Yes, you are already applying the class, but it is overridden by another rule applied on the label which overrides .classname rule anyways. 是的,您已经在应用该类,但是该label已被label上应用的另一条规则覆盖,该规则始终会覆盖.classname规则。 So your syle is not getting applied. 所以您的syle没有得到应用。

Add this to your rule 将此添加到您的规则

label.ac-active {
    border-color:red;
}

Instead of 代替

.ac-active {
        border-color:red;  // if you add !important then it will work too, but not recommended to use that when you have a way to resolve it without it.
    }

And in the script: 并在脚本中:

// Remove class from other tab which is already active and apply to the current tab.
jQuery('.ac-active').not(jQuery(this).addClass("ac-active")).removeClass('ac-active');

Instead of 代替

 jQuery(this).addClass("ac-active");

Demo 演示

What happened in your case is the border color applied by .ac-container label{ overrides the rule applied on the label with the class .ac-active . 您遇到的情况是.ac-container label{应用的边框颜色会覆盖.ac-active类所应用在标签上的规则。 Plus the above script will remove the class from any previously selected tabs ac-active class. 加上上面的脚本将从所有先前选择的选项卡ac-active类中删除该类。

Couple of good reads; 几本好书; this and this 这个这个

Here is the example : Demo 这是示例: 演示

What you need just change the css of "ac-active" increase his weight: 您只需更改“ ac-active”的CSS即可增加他的体重:

.ac-container label.ac-active {border-color:red;}

Updated Script: 更新的脚本:

mainElement= ".alt_block_wrapper .ac-container div";
$(mainElement).find("article").css("display", "none").first().css("display", "block").prev().addClass('ac-active'); 

$(mainElement).find("label").click(function() {
    $this=$(this);
    $(mainElement).find("article").hide(); // Hide all Elements
    $(mainElement).find("label").removeClass('ac-active'); // Hide all Elements
    $this.addClass('ac-active').parent().find("article").fadeIn("slow"); //current parent element
});

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

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