在回调中重新初始化 jquery 手风琴
我的手风琴在页面加载时工作正常,但是在用户与页面上的某些元素交互时,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在用数据重新填充之前销毁手风琴。
Destroy the accordian before repopulating it with data.
对我调用的任何更改:
$('#accordion').accordion( "refresh" )
您应该避免在每次更改时销毁和创建手风琴,它没有意义,并且对性能不利。
这适用于所有情况:添加、删除或重新填充数据(甚至未更改的数据),并且手风琴元素将重新调整自身。
刷新(文档):
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) :