首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在锚内有跨度标签的折叠手风琴

在锚内有跨度标签的折叠手风琴
EN

Stack Overflow用户
提问于 2015-09-21 15:49:45
回答 1查看 1.2K关注 0票数 0

我有一个手风琴网站,在激活手风琴内容的按钮中,有一个带有图标的span标记。通常,我会使用简单的CSS添加图标,但这是一个独特的网站,我不能使用标准技术,因为它必须嵌入的方式。

因此,我不得不添加一个跨度标签,我已经建立了手风琴,这是大部分工作。如果您单击标题面板链接上的任何位置,它就会展开和折叠。

我的问题是,如果您单击span图标区域。它总是扩展内容区域,从不折叠内容区域。因此,例如,如果内容区域被展开,而我单击图标将其折叠,内容就会折叠,然后再次展开。

我有一个JSFiddle显示我在哪里和我遇到的问题

代码语言:javascript
复制
<div class="accordion">
<div class="accordion-section">
    <a class="accordion-section-title" href="#accordion-1">Panel 1 <span class="has-icon">icon</span></a>
    <div id="accordion-1" class="accordion-section-content">
        <p>Content displayed for the <b>first</b> accordion panel.</p>
    </div>
</div>
<div class="accordion-section">
    <a class="accordion-section-title" href="#accordion-2">Panel 2 <span class="has-icon">Icon</span></a>
    <div id="accordion-2" class="accordion-section-content">
        <p>Content displayed for the <b>second</b> accordion panel.</p>
    </div>
</div>
<div class="accordion-section">
    <a class="accordion-section-title" href="#accordion-3">Panel 3 <span class="has-icon">Icon</span></a>
    <div id="accordion-3" class="accordion-section-content">
        <p>Content displayed for the <b>third</b> accordion panel.</p>
    </div>
</div>

jQuery

代码语言:javascript
复制
function close_accordion_section() {
    $('.accordion .accordion-section-title').removeClass('active');
    $('.accordion .accordion-section-content').slideUp(300).removeClass('open');
}
$('.accordion-section-title').click(function(e) {
    // Grab current anchor value
    var currentAttrValue = $(this).attr('href');
    if($(e.target).is('.active')) {
        close_accordion_section();
    } else {
        close_accordion_section();
        // Add active class to section title
        $(this).addClass('active');
        // Open up the hidden content panel
        $('.accordion ' + currentAttrValue).slideDown(300).addClass('open');
    }
    e.preventDefault();
 });

CSS

代码语言:javascript
复制
a.accordion-section-title {
    display: block;
    margin: 0;
    padding: 10px 15px;
    border: 1px solid #CCC;
    background-color: #AAA;
}
a.accordion-section-title:hover {
    background-color: #CCC;
}
a.accordion-section-title span {
    position: absolute;
    right: 20px;
}
.accordion-section-content {
    display: none;
    margin: 0;
    padding: 5px 15px;
    background-color: #EEE;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-21 16:01:55

$(e.target)在单击span时不是“活动的”。如果你改变了

if($(e.target).is('.active')) {if($(e.target).closest('a').is('.active')) {

手风琴如预期的那样工作。小提琴

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32699482

复制
相关文章

相似问题

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