将元素添加到 jquery 的手风琴小部件
我有一个 jquery-ui 手风琴小部件,列出了工作正常的项目部分(它们也是可排序的)。但用户可以使用 html 输入元素添加项目部分。
我想将该新元素添加到手风琴小部件中。
我的方法如下:销毁手风琴和可排序小部件,然后克隆小部件的第一个元素并将其添加到正在克隆的元素之前,以便新元素将成为第一个。之后,只需再次使小部件成为手风琴并可排序即可。
它确实重新创建了小部件,但不包含添加的元素!我不明白为什么不......
html 被转换为小部件:
<div id="accordion">
<div>
<h3><a href="#">Part</a></h3>
<div>
Some explaining text
</div>
</div>
</div>
javascript/jquery 使手风琴在第一次加载时也使可排序和手风琴一起工作良好(停止功能)
var stop = false;
$('#accordion h3').click(function(e) {
if (stop) {
e.stopImmediatePropagation();
e.preventDefault();
stop = false;
}
});
$('#accordion')
.accordion({
header: '> div > h3',
fillSpace: true
})
.sortable({
axis: 'y',
handle: 'h3',
stop: function() {
stop = true;
}
});
:将元素克隆并添加到小部件并重新创建小部件的 javascript:
$('#accordion').accordion("destroy").sortable("destroy");
$("#accordion > div:first").clone().prependTo("#accordion > div:first");
$('#accordion')
.accordion({
header: '> div > h3',
fillSpace: true
})
.sortable({
axis: 'y',
handle: 'h3',
stop: function() {
stop = true;
}
});
I have a jquery-ui accordion widget listing project parts which works fine (they are also sortable). But a user might add an project part by using a html input element.
I would like to prepend that new element to the accordion widget.
My approach is the following: destroy the accordion and sortable widget, then clone the first element of the widget and prepend it to element being cloned so the new element will be the first. After that just make the widget again accordion and sortable.
It does recreate the widget, but it doesn't include the added element! And I don't see why not...
The html being converted to a widget:
<div id="accordion">
<div>
<h3><a href="#">Part</a></h3>
<div>
Some explaining text
</div>
</div>
</div>
The javascript/jquery which makes the accordion on first load and also makes the sortable and accordion working good together (stop function):
var stop = false;
$('#accordion h3').click(function(e) {
if (stop) {
e.stopImmediatePropagation();
e.preventDefault();
stop = false;
}
});
$('#accordion')
.accordion({
header: '> div > h3',
fillSpace: true
})
.sortable({
axis: 'y',
handle: 'h3',
stop: function() {
stop = true;
}
});
The javascript that clones and prepends the element to the widget and recreates the widget:
$('#accordion').accordion("destroy").sortable("destroy");
$("#accordion > div:first").clone().prependTo("#accordion > div:first");
$('#accordion')
.accordion({
header: '> div > h3',
fillSpace: true
})
.sortable({
axis: 'y',
handle: 'h3',
stop: function() {
stop = true;
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,有时解决方案可能非常明显。问题在于使用 prependTo() 函数。应该是: insertBefore() /已解决
Okay, sometimes the solution can be very obvious. The problem was lying in using the prependTo() function. It should be: insertBefore() /Solved