Pour une raison quelconque, le script ci-dessous ne parvient pas à obtenir l'ID des divs draggable en utilisant attr('id')
, mais il fonctionne sur les autres éléments statiques de la page. Je suis totalement confus quant à pourquoi cela ne fonctionnera pas et si quelqu'un a une solution pour moi, il me serait très reconnaissant.Impossible d'obtenir l'ID des divs draggable dans jQuery (à l'aide de jQuery UI)
$(document).ready(function(){
//$(".draggable").draggable();
$(".draggable").draggable({ containment: '#container', scroll: false });
$(".draggable").draggable({ stack: { group: '#container', min: 1 } });
$("*", document.body).click(function (e) {
var offset = $(this).offset();// get the offsets of the selected div
e.stopPropagation();
var theId = $(this).attr('id');// get the id of the selceted div
$("#result").text(this.tagName + " id=" + theId + " (" + offset.left + "," + offset.top +")");
$.post("http://localhost/index.php", "id=" + theId + "&x=" + offset.left + "&y=" + offset.top); //post x,y to php (and the id of the elemnt)
});
var req = function() {
$.ajax({
url: "out.php",
cache: false,
success: function(html){
$("#stuff").empty().append(html);
var css_attr = html.split(",");
$('#1').css('left', css_attr[0] + 'px').css('top', css_attr[1] + 'px');
},
complete: function(){
req();
}
});
};
req();
});
Remarque: Ce script dépend des sources JavaScript suivantes.
- jquery.js
- http://jqueryui.com/latest/ui/ui.core.js
- http://jqueryui.com/latest/ui/ui.draggable.js
- http://jqueryui.com/latest/ui/ui.droppable.js
J'ai déplacé la fonction de propagation et cela fonctionne de la même manière. La fonction click fonctionne également pour certains éléments, mais pas pour ceux qui peuvent être déplacés. –