附加了新的 div,但它们不可拖动?

发布于 2024-08-23 20:54:08 字数 180 浏览 2 评论 0原文

我是一个新手,在使新的 div 可拖动方面遇到一些问题。

示例是: http://jsbin.com/iyexi3/

问题是当我显示对话框时名称,我创建了一些 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 技术交流群。

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

发布评论

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

评论(3

恋竹姑娘 2024-08-30 20:54:08

通过执行以下操作,您已经将一些元素硬编码在 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.

你是年少的欢喜 2024-08-30 20:54:08

问题是,当您将新元素附加到 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.

落日海湾 2024-08-30 20:54:08

最后找到解决方案是在附加新的 div 之后...

$('#nv_objeto').click(function(){   
    var nbr =prompt("Por favor escriba el nombre del Objeto:","");
    $("#layout").append("<div id='"+c_nbr(nbr)+"' class='seleccionable' style='top: 300px; left: 400px; width : 40px; height : 40px; background-color : black;  position :absolute;'></div>");
    $('#layout > div:last').blur().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
    });
});

感谢您的帮助。

finally find the solution is afther append the new div...

$('#nv_objeto').click(function(){   
    var nbr =prompt("Por favor escriba el nombre del Objeto:","");
    $("#layout").append("<div id='"+c_nbr(nbr)+"' class='seleccionable' style='top: 300px; left: 400px; width : 40px; height : 40px; background-color : black;  position :absolute;'></div>");
    $('#layout > div:last').blur().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
    });
});

thanks for the help.

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