我想自动启用' draggable'和' droppable'动态创建的元素。我想在将元素添加到DOM之后重新初始化它。
例如:
$('static_element_such_as_body').on('event', 'dynamicElement', function(){
console.log('yayyyy');
});
这是一个真正动态的事件处理程序。我怎样才能恭维'可拖动的'和' droppable'到.on或.live活页夹?
我可以使用拖放的代码:
$('.draggable').draggable({
helper: function() { //helper function is used to duplicate the event dragged
return $('<p>winning</p>').append($(this).find('.name').clone()); //Idk what class or where .name is but without it, it bombs out
},
stack: ".draggable", //this changes our z index to be the upper most
start: function(event, ui) { //this fires on start
c.tr = this; //set up our c cariable
c.helper = ui.helper;
}
}).droppable({ //this let's us know this element is droppable
drop: function(event, ui) { //when you actually drop
$(c.helper).remove();
},
over: function (event, ui) { //called when we are hovering over a droppable element with this selector
console.log('over');
},
out: function(event, ui){
console.log('left');
}
});
答案 0 :(得分:0)
显然没有人知道,所以我想出了一个更自动化的&#39;解。您可以使用此事件处理程序自动为您重新初始化,而不是在添加元素后手动重新初始化它。
$mainContainer.on('DOMNodeInserted', function(e){
if($.inArray('relatable', e.target.classList) != -1){
drag_drop_init();
if(helper_bool_isRelatableEnabled){
drag_drop_enable();
}else{
drag_drop_disable();
}
}
});
其中$ mainContainer是一个静态元素,例如$(&#39; body&#39;)和&#39; relatable&#39;是我用来知道这个元素应该允许拖放的类的名称。它是我用来初始化的类ie:$(&#39; .relatable&#39;)。draggable()