将内部手风琴创建为空手风琴

发布于 2024-11-08 20:50:23 字数 3547 浏览 1 评论 0原文

我对内部手风琴有一个问题:添加一些面板后刷新手风琴的 destroy-recreate 方法无法正常工作。

当我单击第二个手风琴中的按钮时,它会在其中添加一个部分。但是子手风琴不起作用(尽管对手风琴进行了破坏重建)。 您可以在此处查看代码并进行测试: http://jsfiddle.net/YL99D/2/

但是,如果我通过在 #sections div 中添加初始部分来修改 HTML 代码,那么当我单击按钮时它会完美地工作(此处的代码和演示:http://jsfiddle.net/YL99D/3/

我还查看了使用 Firebug 生成的 HTML 代码,发现了一些有趣的东西。 首先看一下当我在 #sections div 中放置硬编码部分时生成的代码:

<div id="sections" class="ui-accordion ui-widget ui-helper-reset ui-accordion-icons" role="tablist">
    <div id="sections_new1">
        <h3 class="ui-accordion-header ui-helper-reset ui-state-active ui-corner-top" role="tab" aria-expanded="true" aria-selected="true" tabindex="0">
            <span class="ui-icon ui-icon-triangle-1-s"></span>
            <a href="#" tabindex="-1">Section</a>
        </h3>
        <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" role="tabpanel" style="display: block;">
            Section content :
            <br>
            <div class="sub-accordions ui-accordion ui-widget ui-helper-reset ui-accordion-icons" role="tablist">
                <div>
                    <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-corner-all ui-state-hover" role="tab" aria-expanded="false" aria-selected="false" tabindex="0">
                        <span class="ui-icon ui-icon-triangle-1-e"></span>
                        <a href="#" tabindex="-1">Sub accordion.</a>
                    </h3>
                    <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" role="tabpanel" style="display: none;">Sub accordion content</div>
                </div>
            </div>
        </div>
    </div>
</div> 

现在看一下当我让 #sections div 为空时生成的代码:

<div id="sections" class="ui-accordion ui-widget ui-helper-reset ui-accordion-icons" role="tablist">
    <div id="sections_new1">
        <h3 class="ui-accordion-header ui-helper-reset ui-state-active ui-corner-top" role="tab" aria-expanded="true" aria-selected="true" tabindex="0">
            <span class="ui-icon ui-icon-triangle-1-s"></span>
            <a href="#" tabindex="-1">Section</a>
        </h3>
        <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" role="tabpanel" style="display: block;">
            Section content :
            <br>
            <div class="sub-accordions ui-accordion ui-widget ui-helper-reset ui-accordion-icons" role="tablist">
                <div class="ui-accordion-header ui-helper-reset ui-state-default ui-state-active ui-corner-top ui-state-hover" role="tab" aria-expanded="true" aria-selected="true" tabindex="0">
                    <span class="ui-icon ui-icon-triangle-1-s"></span>
                    <h3>
                        <a href="#" tabindex="-1">Sub accordion.</a>
                    </h3>
                    <div>Sub accordion content</div>
                </div>
            </div>
        </div>
    </div>
</div> 

结果是类设置错误。 有人知道是什么导致了这个问题吗?

[编辑]:完整改写

I have a problem with inner-accordions : the destroy-recreate method to refresh an accordion after adding some panels doesn't work correctly.

When I click on the button in the second accordion it adds a section into it. But the sub-accordion doesn't work (in spite of the desctruction-recreation of the accordion).
You can see the code and test it here : http://jsfiddle.net/YL99D/2/

BUT if I modify the HTML code by adding an initial section in the #sections div, it works perfectly when I click the button (Code and demo here : http://jsfiddle.net/YL99D/3/ )

I have also looked at the generated HTML codes using Firebug and I have found something interesting.
First look at the code generated when I have put a hard-coded section in the #sections div :

<div id="sections" class="ui-accordion ui-widget ui-helper-reset ui-accordion-icons" role="tablist">
    <div id="sections_new1">
        <h3 class="ui-accordion-header ui-helper-reset ui-state-active ui-corner-top" role="tab" aria-expanded="true" aria-selected="true" tabindex="0">
            <span class="ui-icon ui-icon-triangle-1-s"></span>
            <a href="#" tabindex="-1">Section</a>
        </h3>
        <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" role="tabpanel" style="display: block;">
            Section content :
            <br>
            <div class="sub-accordions ui-accordion ui-widget ui-helper-reset ui-accordion-icons" role="tablist">
                <div>
                    <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-corner-all ui-state-hover" role="tab" aria-expanded="false" aria-selected="false" tabindex="0">
                        <span class="ui-icon ui-icon-triangle-1-e"></span>
                        <a href="#" tabindex="-1">Sub accordion.</a>
                    </h3>
                    <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" role="tabpanel" style="display: none;">Sub accordion content</div>
                </div>
            </div>
        </div>
    </div>
</div> 

Now look at the code generated when I let the #sections div empty :

<div id="sections" class="ui-accordion ui-widget ui-helper-reset ui-accordion-icons" role="tablist">
    <div id="sections_new1">
        <h3 class="ui-accordion-header ui-helper-reset ui-state-active ui-corner-top" role="tab" aria-expanded="true" aria-selected="true" tabindex="0">
            <span class="ui-icon ui-icon-triangle-1-s"></span>
            <a href="#" tabindex="-1">Section</a>
        </h3>
        <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" role="tabpanel" style="display: block;">
            Section content :
            <br>
            <div class="sub-accordions ui-accordion ui-widget ui-helper-reset ui-accordion-icons" role="tablist">
                <div class="ui-accordion-header ui-helper-reset ui-state-default ui-state-active ui-corner-top ui-state-hover" role="tab" aria-expanded="true" aria-selected="true" tabindex="0">
                    <span class="ui-icon ui-icon-triangle-1-s"></span>
                    <h3>
                        <a href="#" tabindex="-1">Sub accordion.</a>
                    </h3>
                    <div>Sub accordion content</div>
                </div>
            </div>
        </div>
    </div>
</div> 

The result is that classes are badly set.
Do someone have an idea of what is causing this problem ?

[EDIT] : Complete rephrasing

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

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

发布评论

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

评论(2

辞取 2024-11-15 20:50:23

Accordion 仅在启动时更新 css,而不是在动态添加内容时更新。当您想要添加(或减去)某些内容时,最好的选择可能是执行以下操作:

var activeEl = $('#questions').accordion('option','active');

$('#questions').append('<h3><a href="#">foo</a></h3>');
$('#questions').append('<div>bar</div>');

$('#questions').accordion('destroy').accordion({active:activeEl});

添加:
顺便说一句,这里是一个小提琴示例: http://jsfiddle.net/NkVQJ/

Accordion only updates the css on startup, not when you add something dynamically to it. Your best bet may be to to do something like this when you want to add (or substract) something:

var activeEl = $('#questions').accordion('option','active');

$('#questions').append('<h3><a href="#">foo</a></h3>');
$('#questions').append('<div>bar</div>');

$('#questions').accordion('destroy').accordion({active:activeEl});

Added:
Here, btw, is an example fiddle: http://jsfiddle.net/NkVQJ/

情释 2024-11-15 20:50:23

编辑:我找到了问题的根源。
以下是代码和演示: http://jsfiddle.net/YL99D/5/

如果您有我遇到过同样的问题,当手风琴确实存在并且不为空时,请注意创建它们。否则似乎会引起很多问题。
此外,我使用对象来存储手风琴的选项,而不是使用方法 data()。因此,即使在加载 DOM 后添加 HTML 元素,手风琴似乎也会以正常方式做出反应。


我已经部分找到了解决方案。无需定义与选择器相关的数据,只需定义包含选项的对象即可。

以下是代码和演示: http://jsfiddle.net/YL99D/4/

但确实如此当我们将子手风琴添加到现有的(不是 javascript 生成的)手风琴中时,该功能不起作用。

所以问题还是没有解决

EDIT : I have found the source of the problem.
Here are the code and demo : http://jsfiddle.net/YL99D/5/

If you have the same problem I've had, take care of creating accordions when they really exist and are not empty. Otherwise it seems it causes many problems.
Moreover, I have used objects to store accordions' options, instead of using the method data(). Thus, the accordions seem to react a normal way even after adding HTML elements after the DOM has been loaded.


I have partially found a solution. Instead of defining data related to the selectors, simply define objects which contain the options.

Here are the code and demo : http://jsfiddle.net/YL99D/4/

BUT it does not work when we add a sub-accordion into an existing (not javascript-generated) accordion.

So the problem is still no solved

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