There are some div elements on my page. A user can drag any of them many times. After every dragging, I need to get new coordinates of the div that was dragged.
我的页面上有一些div元素。用户可以多次拖动它们中的任何一个。每次拖动后,我都需要获取被拖动的div的新坐标。
My code works good with div[0]: I actually get new coordinates after every new dragging. The problem is with all the other divs, like div[1], div[2], div[10]... Script gets coordinates after the first dragging, but all the next times coordinates are still the same. They don't change.
我的代码适用于div [0]:我实际上在每次新的拖动后都会得到新的坐标。问题在于所有其他div,如div [1],div [2],div [10] ......脚本在第一次拖动后获取坐标,但所有下一次坐标仍然相同。他们不会改变。
I tried to clear variables, but that didn't help.
我试图清除变量,但这没有帮助。
What could be this problem caused by? What should I check to find the solution?
这个问题可能是由什么引起的?我应该检查什么来找到解决方案?
I use jQuery and jQuery UI. Code:
我使用jQuery和jQuery UI。码:
$(document).ready(function(){
$(".rD").draggable({
stop: function(event, ui) {
// get the index of the dragged element
id = $(this).index();
// get coordinates of the dragged element
rect = document.getElementsByTagName("div")[id].getBoundingClientRect();
alert("top:" + rect.top + ", left: " + rect.left);
// clearing variables didn't help to solve the problem
delete rect;
delete id;
}
});
1 个解决方案
#1
Try to:
alert(ui.position.top, ui.position.left)
Whole code:
$(document).ready(function(){
$(".rD").draggable({
stop: function(event, ui) {
// Use var keyword for your local variables
// var rect = ui.helper[0].getBoundingClientRect();
alert("top:" + ui.position.top + ", left: " + ui.position.left);
}
});
});
#1
Try to:
alert(ui.position.top, ui.position.left)
Whole code:
$(document).ready(function(){
$(".rD").draggable({
stop: function(event, ui) {
// Use var keyword for your local variables
// var rect = ui.helper[0].getBoundingClientRect();
alert("top:" + ui.position.top + ", left: " + ui.position.left);
}
});
});