jQuery - 获取删除元素的相对位置和属性

发布于 2024-10-10 06:23:25 字数 956 浏览 5 评论 0原文

我有可拖动的元素,可以将其放置在可放置区域中。如果元素被删除,则调用 drop 函数:

$('#droppable').droppable({
    scope: "items",
    drop: function (event, ui) {
        // this one is called if an element is dropped in this droppable area
    }
});

我的可拖动元素:

<div class="drag" data-noteid="10">Drag me</div>
...
$('.drag').draggable({
    revert: "invalid",
    scope: "items"
});

如果元素被删除,我需要知道的是 data-noteid 的值和相对值可放置区域的位置。因此,如果将元素放在左上角,则 x/y 坐标必须为 0/0。

我在这里创建了一个完整的工作示例: http://jsbin.com/utivo5/2/

所以通常我可以像这样访问属性:

alert($(this).data("noteid"));

alert($(this).position().top);
alert($(this).position().left);

但在这种情况下我得到的只是未定义

有谁知道我如何访问它们?我认为使用 eventui 一定是可能的,它是被调用的 drop 函数的参数?!

预先感谢您&最好的问候,蒂姆。

I have draggable elements which can be dropped in droppable areas. If an element is dropped, the drop function is called:

$('#droppable').droppable({
    scope: "items",
    drop: function (event, ui) {
        // this one is called if an element is dropped in this droppable area
    }
});

My draggable element:

<div class="drag" data-noteid="10">Drag me</div>
...
$('.drag').draggable({
    revert: "invalid",
    scope: "items"
});

What I need to know if the element is dropped is the value of data-noteid and the relative position of the droppable area. So, if the element is dropped on the upper left corner, the x/y coordinates must be 0/0.

I created a full working example here: http://jsbin.com/utivo5/2/

So normally I can access the attributes like this:

alert($(this).data("noteid"));

alert($(this).position().top);
alert($(this).position().left);

but all I get in this case is undefined.

Do anyone know how I can access them? I think it must be possible with event or ui which is a parameter of the called drop function?!

Thank you in advance & Best Regards, Tim.

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

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

发布评论

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

评论(2

醉城メ夜风 2024-10-17 06:23:25

在这种情况下,您希望使用 ui 参数来获取可拖动区域,而不是引用可放置区域的 this ,特别是这里的 ui.draggable 。总体看起来应该是这样的:

drop: function (event, ui) {
  var pos = ui.draggable.offset(), dPos = $(this).offset();
  alert("nodeid: " + ui.draggable.data("noteid") + 
        ", Top: " + (pos.top - dPos.top) + 
        ", Left: " + (pos.left - dPos.left));
}

对于位置,我们需要减去可放置的顶部/左侧位置以获得您想要的值,即上面的 dPos 被删除。

您可以使用此处的上述更改来测试您的演示

In this case you want the ui argument to get the draggable, rather than this which refers to the droppable area, specifically ui.draggable here. It should look like this overall:

drop: function (event, ui) {
  var pos = ui.draggable.offset(), dPos = $(this).offset();
  alert("nodeid: " + ui.draggable.data("noteid") + 
        ", Top: " + (pos.top - dPos.top) + 
        ", Left: " + (pos.left - dPos.left));
}

For the position, we need to subtract the droppable's top/left position to get the value you want here, that's the dPos above getting removed.

You can test your demo with the above changes working here.

℉服软 2024-10-17 06:23:25

我发现根据我的情况,上述方法不起作用。不知道为什么 - 它总是给我一个 X = -7 和 Y = 229。

但这确实有效(位于 drop 函数处理程序中):

alert('X pos in drop parent container = ' + (ui.position.top - $(this).offset().top));

I found for my circumstances that the above didn't work. Not sure why - it always gave me an X = -7 and Y = 229.

But this did work (located in drop function handler):

alert('X pos in drop parent container = ' + (ui.position.top - $(this).offset().top));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文