jQuery - 获取删除元素的相对位置和属性
我有可拖动的元素,可以将其放置在可放置区域中。如果元素被删除,则调用 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);
但在这种情况下我得到的只是未定义
。
有谁知道我如何访问它们?我认为使用 event
或 ui
一定是可能的,它是被调用的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,您希望使用
ui
参数来获取可拖动区域,而不是引用可放置区域的this
,特别是这里的ui.draggable
。总体看起来应该是这样的:对于位置,我们需要减去可放置的顶部/左侧位置以获得您想要的值,即上面的 dPos 被删除。
您可以使用此处的上述更改来测试您的演示。
In this case you want the
ui
argument to get the draggable, rather thanthis
which refers to the droppable area, specificallyui.draggable
here. It should look like this overall: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.
我发现根据我的情况,上述方法不起作用。不知道为什么 - 它总是给我一个 X = -7 和 Y = 229。
但这确实有效(位于 drop 函数处理程序中):
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):