在回调中重新初始化 jquery 手风琴

发布于 2024-08-02 18:55:05 字数 474 浏览 9 评论 0原文

我的手风琴在页面加载时工作正常,但是在用户与页面上的某些元素交互时,我使用 ajax 来重建手风琴的 HTML。然后,我尝试再次重新初始化手风琴,

    $('#accordion').accordion({
        active: false,
        autoHeight: false,
        clearStyle: true,
        collapsible: true,
        header: 'h3'
    });

但是......它似乎没有采取。

新的 HTML 渲染块没有应用折叠规则。相反,它只是作为一个大列表保持打开状态。

我什至尝试过 setTimeout() 来重新调用手风琴,以防它在回调中返回渲染的 HTML 之前尝试初始化。

回调中呈现的 HTML 与页面加载的 HTML 完全相同(Accordion 方法有效时发生的 DOM 添加除外)

I've got the accordion working fine on pageload, however upon user interaction with some elements on the page, I am using ajax to rebuild the HTML of the accordion. Then, I try to re-initialize the accordion again with

    $('#accordion').accordion({
        active: false,
        autoHeight: false,
        clearStyle: true,
        collapsible: true,
        header: 'h3'
    });

but... it doesn't seem to be taking.

The new block of rendered HTML isn't applying the accordion rules. Instead it just stays open as a large list.

I've even tried setTimeout() for re-calling the accordion just incase it was trying to initialize before the rendered HTML is returned in the callback.

The HTML that is rendered in the callback is the exact same as that of the pageload (with exception to the DOM additions that occur when the Accordion method is effective)

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

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

发布评论

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

评论(2

甜味拾荒者 2024-08-09 18:55:05

在用数据重新填充之前销毁手风琴。

$('#accordion').accordion('destroy');

Destroy the accordian before repopulating it with data.

$('#accordion').accordion('destroy');
不必在意 2024-08-09 18:55:05

对我调用的任何更改

$('#accordion').accordion( "refresh" )

您应该避免在每次更改时销毁和创建手风琴,它没有意义,并且对性能不利

这适用于所有情况:添加、删除或重新填充数据(甚至未更改的数据),并且手风琴元素将重新调整自身。

刷新(文档):

处理直接在 DOM 中添加或删除的任何标题和面板,并重新计算折叠面板的高度。结果取决于内容和 heightStyle 选项。

On any change I call:

$('#accordion').accordion( "refresh" )

You should avoid destroying and creating the accordion on each change, it doesn't make sense and it is bad for performance.

This works for all situations: added to, removed from or repopulated your data (or even unchanged data) and the accordion element will readjust itself.

Refresh (Docs) :

Process any headers and panels that were added or removed directly in the DOM and recompute the height of the accordion panels. Results depend on the content and the heightStyle option.

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