开发者

How to auto apply drag and drop effect to dynamically added element?

开发者 https://www.devze.com 2022-12-26 00:36 出处:网络
I use jquery ui to apply a drag and drop effect on a serial of DIVs, for example: <div class=\"draggable\">...</div>

I use jquery ui to apply a drag and drop effect on a serial of DIVs, for example:

<div class="draggable">...</div>
<div class="draggable">...</div>
<div class="draggable">...</div>
<div class="draggable"> this DIV was dynamically added, not draggable </div>

The problem is dynamically added开发者_如何学JAVA DIVs won't have this effect applied, how can i apply this effect on new members too?


You cannot use the .live() function with .draggable() directly, but you can use .live() with the mouseover event and re-attach .draggable() on mouseover like this.

$('.draggable').live('mouseover',function(){
    $(this).draggable();
});


Have a look at the jQuery .live(). I believe you could use it here. If not - just attach .draggable() when you create your element.


You might also like to take a loot at the delegate() method introduced with jQuery 1.4 which is slightly better than live()

Here is an article comparing both approaches - http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-the-difference-between-live-and-delegate/

0

精彩评论

暂无评论...
验证码 换一张
取 消