附加了新的 div,但它们不可拖动?
I'm a newbie and have some problems with making new divs draggable.
The example is: http://jsbin.com/iyexi3/
The problem is when I show the dialog for the name, I create some divs, but they aren't draggable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过执行以下操作,您已经将一些元素硬编码在 HTML 可拖动中:
$('.seleccionable').draggable({
拖动:函数(e,ui){
$('#status_nombre').html($(this).attr('id'));
$('#status_top').attr('value',$(this).css('top'));
$('#status_left').attr('value',$(this).css('left'));
}
,遏制:'#layout'
,refreshPositions: true
,snap: true
});
当您添加一个新元素时,您也需要为该元素调用 .draggable() 。
一种方法是将选择器/可拖动代码放入函数中,并在添加新元素后调用它。
You've made some elements that are hardcoded in the HTML draggable by doing this:
$('.seleccionable').draggable({
drag: function(e, ui) {
$('#status_nombre').html($(this).attr('id'));
$('#status_top').attr('value',$(this).css('top'));
$('#status_left').attr('value',$(this).css('left'));
}
,containment: '#layout'
,refreshPositions: true
,snap: true
});
When you add a new element you need to call .draggable() for that one too.
One way to do it would be to put that selector/draggable code into a function and call it after you've added a new element.
问题是,当您将新元素附加到 DOM 时,它不会继承现有可拖动元素的事件处理程序。
您可能会运气好 liveQuery
这将允许您创建所有当前和未来的实时事件与选择器匹配的元素。它类似于内置的 live() 方法。我还没有使用过它,但它应该与可拖动一起使用。
The issue is that when you append a new element to the DOM, it doesn't inherit the event handlers of the existing draggable elements.
You may have some luck with liveQuery
This will allow you to create live events on all current and future elements matching a selector. It's similar to the built-in live() method. I haven't used it but it should work with draggable.
最后找到解决方案是在附加新的 div 之后...
感谢您的帮助。
finally find the solution is afther append the new div...
thanks for the help.