我有一个拖放代码,在服务器端创建的div上工作得很好,但是当我使用jquery (动态)创建div时,我似乎不能将任何东西放到容器中……
$('.dropcontent').droppable({
accept: '.item',
drop: function(ev, ui) {
/* do something */
}
});
$(".item").draggable({
helper:'clone',
appendTo: 'body',
snap: true,
revert: true
});
<div id="row1seo" class="dropcontent" > </div> // original code on server side
<div id="row1seo" class="dropcontent ui-droppable"> </div> // the above line becomes this on client side showing it has "binded" with the droppable
<div id="row2seo" class="dropcontent"></div> // this the dynamically created div which doesn't seem to bind with the droppable. this is created in php file using ajax to retrieve it
我也试过
$(".dropcontent").live('droppable', function() {
......
});
似乎不工作..。有什么办法解决这个问题吗?
谢谢
发布于 2012-05-11 18:36:09
你必须激活拖动函数,这样它才能在生成的元素上工作。太糟糕了,jQuery的live()
函数不能处理拖放,所以你必须自己创建一个。例如,我使用此函数:
(function ($) {
$.fn.liveDraggable = function (opts) {
this.live("mouseover", function() {
if (!$(this).data("init")) {
$(this).data("init", true).draggable(opts);
}
});
return $();
};
}(jQuery));
这样叫它:
$( "element" ).liveDraggable()
你也可以很容易的为droppable做一个!GL!
https://stackoverflow.com/questions/10549716
复制相似问题