查询类切换以更改跨度背景图像不起作用

发布于 2024-12-04 11:04:59 字数 326 浏览 1 评论 0原文

好吧,我对 jQuery 有足够的了解,可以完成我的一些任务(显然除了这个)。

情况:
我有一个手风琴菜单,其中有加号和减号图像,显示在菜单的左侧。我希望它们在子表可见时改变。

我的代码中有一些错误不允许这种情况发生。当您单击其中一个选项时,它将变为减号;当您单击另一个选项时,它将变为加号。

问题: 扩展区域后,如果再次单击可见选项来折叠元素,手风琴类将不会更改。

这是一个 jsfiddle 演示 http://jsfiddle.net/mKUNs/

Ok, I know enough about jQuery to get some of my tasks accomplished (well except this one apparently).

The situation:
I have an accordion menu that has plus and minus images that are shown in the left hand side of the menu. I want them to change when the child table is visible.

there is some bug in my code that won't allow this to happen. it will change to the minus when you click one of the options and it will change to the plus when you click another option.

The Problem:
Once an area is expanded the accordion class won't change if you click the visible option again to collapse the element.

Here is a jsfiddle demo
http://jsfiddle.net/mKUNs/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔嚣张 2024-12-11 11:04:59

这是一个固定版本但不要使用它。

您的文档结构非常糟糕。您的表格只有一个单元格,标题和可切换部分之间的关​​联仅通过相邻关系进行。您最好更改标记,以便每个手风琴都被

/

包围。

此外,您已经不再使用 id,并且您的 CSS 都应该从文档中删除。不应使用align="center"。最后,为什么要为 expanded 和 collapsed 建立一个类来搞乱事情呢?两者是互斥的,所以只使用一个类!


你应该这样做

HTML

<div class='accordian'>
    <h3><span class='icon'></span>Option 1</h3>
    <div>
        the box should be black now, if you click this option
        again it should turn white
    </div>
</div>
<div class='accordian' id='weekly'>
    <h3><span class='icon'></span>Option 2</h3>
    <div>
        the box should be black now, if you click this option
        again it should turn white
    </div>
</div>

CSS:

.accordian h3 {
    background-color:#689937;
    color:#fff;
    height:30px;
}
.accordian .icon {
    background-color:#fff;
    background-size:25px;
    background-repeat: no-repeat;
    height:25px;
    width:25px;
    padding-left:5px;
    float:left;
}
.accordian.collapsed .icon {
    background-color:#000;
}

jQuery

$('.accordian').each(function() {
    var accordian = $(this);
    var header = accordian.find('h3');
    var content = accordian.find('div');

    header.click(function() {
        content.slideToggle('medium');
        accordian.toggleClass('collapsed');
    });

    content.hide();
    accordian.addClass('collapsed');
});

Here's a fixed version. But don't use it.

Your document is structured very badly. You've got tables with only one cell, and association between the header and the toggleable section is only by adjacency. You'd do better to change your markup so that each accordian is surrounded by a <div>/<section>.

Additionally, you're over using ids, and your CSS should all be taken out of the document. align="center" should not be used. Lastly, why muddle things by having a class for expanded and collapsed? The two are mutually exclusive, so just use one class!


This is how you should do it:

HTML

<div class='accordian'>
    <h3><span class='icon'></span>Option 1</h3>
    <div>
        the box should be black now, if you click this option
        again it should turn white
    </div>
</div>
<div class='accordian' id='weekly'>
    <h3><span class='icon'></span>Option 2</h3>
    <div>
        the box should be black now, if you click this option
        again it should turn white
    </div>
</div>

CSS:

.accordian h3 {
    background-color:#689937;
    color:#fff;
    height:30px;
}
.accordian .icon {
    background-color:#fff;
    background-size:25px;
    background-repeat: no-repeat;
    height:25px;
    width:25px;
    padding-left:5px;
    float:left;
}
.accordian.collapsed .icon {
    background-color:#000;
}

jQuery

$('.accordian').each(function() {
    var accordian = $(this);
    var header = accordian.find('h3');
    var content = accordian.find('div');

    header.click(function() {
        content.slideToggle('medium');
        accordian.toggleClass('collapsed');
    });

    content.hide();
    accordian.addClass('collapsed');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文