我有一个使用一组嵌套的 Accordion 实现的菜单,12,每个元素都带有ab
我想实现以下逻辑:

  • 当我单击1a时,我将获得1a的数据和两个子菜单2a2b
  • 当我单击2a2b时,我将分别获取每个数据。

  • 问题
    所需结果:
  • 我只想显示最后一次单击的nth-most子元素,折叠所有其他元素。
  • 初始化后,仅1a1b应该是可见的。

  • 当前结果:
  • 单击1b,然后单击2b1b仍然完全可见。

  • JavaScript代码
    $(document).ready(function() {
        $("#acc1").accordion({
            active:".ui-accordion-left",
            alwaysOpen: false,
            autoheight: false,
            header: 'a.acc1',
            clearStyle: true
        });
        $("#acc2").accordion({
            alwaysOpen: false,
            autoheight: false,
            header: 'a.acc2',
            clearStyle: true
        });
    });
    
    HTML:
    <ul id="acc1" class="ui-accordion-container">
        <li>
            <div class="ui-accordion-left"></div>
            <a class="ui-accordion-link acc1">1a
                <span class="ui-accordion-right"></span>
            </a>
            <div>
                data of 1a<br/>
                data of 1a<br/>
                data of 1a<br/>
            </div>
            <div>
                <ul class="ui-accordion-container" id="acc2">
                    <li>
                        <div class="ui-accordion-left"></div>
                        <a class="ui-accordion-link acc2">2a
                            <span class="ui-accordion-right"></span>
                        </a>
                        <div>
                            data of 2a<br/>
                            data of 2a<br/>
                            data of 2a<br/>
                        </div>
                    </li>
                    <li>
                        <div class="ui-accordion-left"></div>
                        <a class="ui-accordion-link acc2">2b
                            <span class="ui-accordion-right"></span>
                        </a>
                        <div>
                            data of 2b<br/>
                            data of 2b<br/>
                            data of 2b<br/>
                        </div>
                    </li>
                </ul>
            </div>
        </li>
        <li>
            <div class="ui-accordion-left"></div>
            <a class="ui-accordion-link acc1">1b
                <span class="ui-accordion-right"></span>
            </a>
            <div>
                data of 1b<br />
                data of 1b<br />
                dta of 1b <br />
            </div>
        </li>
    </ul>
    

    最佳答案

    只需更改HTML中元素的顺序,即可获得所需的行为。现在开始时仅打开1a和1b。同样,当您单击1b时,它将关闭1a,这也将隐藏任何打开的2a/2b部分。

        $(document).ready(function() {
            $("#acc1").accordion({
                active:".ui-accordion-left",
                alwaysOpen: false,
                autoheight: false,
                header: 'a.acc1',
                clearStyle: true
            });
            $("#acc2").accordion({
                active:".ui-accordion-left",
                alwaysOpen: false,
                autoheight: false,
                header: 'a.acc2',
                clearStyle: true
            });
        });
    
        <ul id="acc1" class="ui-accordion-container">
            <li>
                <div class="ui-accordion-left">
                </div>
                <a class="ui-accordion-link acc1">1a
                    <span class="ui-accordion-right"></span>
                </a>
                <div>
                    data of 1a<br/>
                    data of 1a<br/>
                    data of 1a<br/>
                    <ul class="ui-accordion-container" id="acc2">
                        <li>
                            <div class="ui-accordion-left">
                            </div>
                            <a class="ui-accordion-link acc2">2a
                                <span class="ui-accordion-right"></span>
                            </a>
                            <div>
                                data of 2a<br/>
                                data of 2a<br/>
                                data of 2a<br/>
                            </div>
                        </li>
                        <li>
                            <div class="ui-accordion-left">
                            </div>
                            <a class="ui-accordion-link acc2">2b
                            <span class="ui-accordion-right"></span></a>
                            <div>
                                data of 2b<br/>
                                data of 2b<br/>
                                data of 2b<br/>
                            </div>
                        </li>
                    </ul>
                </div>
            </li>
            <li>
                <div class="ui-accordion-left"></div>
                <a class="ui-accordion-link acc1">1b
                <span class="ui-accordion-right"></span></a>
                <div>
                    data of 1b<br />
                    data of 1b<br />
                    dta of 1b <br />
                </div>
            </li>
        </ul>
    </body>
    

    关于jquery - jQuery中的嵌套 Accordion 菜单,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/479270/

    10-09 17:41