首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在第二次单击时从元素中删除类

如何在第二次单击时从元素中删除类
EN

Stack Overflow用户
提问于 2018-07-17 05:15:42
回答 1查看 31关注 0票数 0

我试图在点击一个按钮时切换内容信息块,当我点击另一个按钮时,这是有效的,但当我点击同一个按钮时,我希望隐藏信息,但它目前没有任何作用。

这两个部分由目标属性连接,以便在单击目标按钮时显示正确的内容块。

下面是我的代码:

HTML:

代码语言:javascript
复制
<div class="cce-equipment-row">
    <div class="cce-equipment-column">
        <div class="cce-equipment-image-container">
            <img src="{{root}}static/images/machines/newco-20-1-digital-drip-brewer.png" alt="Newco 20:1 Digital Drip Brewer">
        </div>
        <p>NEWCO 20:1 DIGITAL DRIP<br/>BREWER</p>
        <span class="product-toggle-button" target="25">+</span>
    </div>
    <div class="cce-equipment-column">
        <div class="cce-equipment-image-container">
            <img src="{{root}}static/images/machines/nuova-simonelli-appia-espresso-machine.png" alt="Nuova Simonelli Appia Espresso Machine">
        </div>
        <p>NUOVA SIMONELLI APPIA<br/>ESPRESSO MACHINE</p>
        <span class="product-toggle-button" target="26">+</span>
    </div>
    <div class="cce-equipment-column">
        <div class="cce-equipment-image-container">
            <img src="{{root}}static/images/machines/nuovo-simonelli-mdx-grinder.png" alt="Nuova Simonelli MDX Grinder">
        </div>
        <p>NUOVA SIMONELLI MDX<br/>GRINDER</p>
        <span class="product-toggle-button" target="27">+</span>
    </div>
    <div class="cce-equipment-column">
        <div class="cce-equipment-image-container">
            <img src="{{root}}static/images/machines/starbucks-digital-interactive-bean-to-cup-brewer.png" alt="Newco Eco Series Drip Brewerq">
        </div>
        <p>STARBUCKS DIGITAL INTERACTIVE<br/>BEAN TO CUP BREWER</p>
        <span class="product-toggle-button" target="28">+</span>
    </div>
</div>
<div id="cce-equipment-info25" class="cce-equipment-info ams-multi-tasker-combo">
    <h1>NEWCO 20:1 DIGITAL DRIP BREWER</h1>
    <h2>Perfectly designed, right down to the latte macchiato</h2>
    <p>The IMPRESSA XJ9 Professional cuts an outstanding figure where the quality of the drinks served is just as important as aesthetics in architecture, ambience and design.</p>
    <p>It creates perfect latte macchiato, cappuccino, café crème, espresso and ristretto at the touch of a button.</p>
    <p>Meanwhile, the clear and symmetrical design, sophisticated chrome-plated bean container and brilliant silver finish add a touch of charisma.</p>
    <a href="#"><span>Download PDF Specs</span></a>
</div>
<div id="cce-equipment-info26" class="cce-equipment-info ams-wide-gem-snack">
    <h1>NUOVA SIMONELLI APPIA ESPRESSO MACHINE</h1>
    <h2>Perfectly designed, right down to the latte macchiato</h2>
    <p>The IMPRESSA XJ9 Professional cuts an outstanding figure where the quality of the drinks served is just as important as aesthetics in architecture, ambience and design.</p>
    <p>It creates perfect latte macchiato, cappuccino, café crème, espresso and ristretto at the touch of a button.</p>
    <p>Meanwhile, the clear and symmetrical design, sophisticated chrome-plated bean container and brilliant silver finish add a touch of charisma.</p>
    <a href="#"><span>Download PDF Specs</span></a>
</div>
<div id="cce-equipment-info27" class="cce-equipment-info crane-bev-max-media">
    <h1>NUOVA SIMONELLI MDX GRINDER</h1>
    <h2>Perfectly designed, right down to the latte macchiato</h2>
    <p>The IMPRESSA XJ9 Professional cuts an outstanding figure where the quality of the drinks served is just as important as aesthetics in architecture, ambience and design.</p>
    <p>It creates perfect latte macchiato, cappuccino, café crème, espresso and ristretto at the touch of a button.</p>
    <p>Meanwhile, the clear and symmetrical design, sophisticated chrome-plated bean container and brilliant silver finish add a touch of charisma.</p>
    <a href="#"><span>Download PDF Specs</span></a>
</div>
<div id="cce-equipment-info28" class="cce-equipment-info crane-merchant-media-snack">
    <h1>STARBUCKS DIGITAL INTERACTIVE BEAN TO CUP BREWER</h1>
    <h2>Perfectly designed, right down to the latte macchiato</h2>
    <p>The IMPRESSA XJ9 Professional cuts an outstanding figure where the quality of the drinks served is just as important as aesthetics in architecture, ambience and design.</p>
    <p>It creates perfect latte macchiato, cappuccino, café crème, espresso and ristretto at the touch of a button.</p>
    <p>Meanwhile, the clear and symmetrical design, sophisticated chrome-plated bean container and brilliant silver finish add a touch of charisma.</p>
    <a href="#"><span>Download PDF Specs</span></a>
</div>

JS:

代码语言:javascript
复制
$('.product-toggle-button').click(function(e) {
    $('.product-toggle-button--open').removeClass('product-toggle-button--open');
    $(this).toggleClass('product-toggle-button--open');
    $('.cce-equipment-info').hide();
    $('#cce-equipment-info'+$(this).attr('target')).show();
});
EN

回答 1

Stack Overflow用户

发布于 2018-07-17 05:19:33

这是因为如果--open类存在,您的removeClass也会从current按钮中删除。

那么toggleClass将始终只充当addClass

尝试更改为:

代码语言:javascript
复制
 $('.product-toggle-button--open').not(this).removeClass('product-toggle-button--open');
                                  // ^^ exclude current button
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51370221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档