开发者

Clone a div and then clone again from the child

I have the following simple code, where I double click a div (wi开发者_高级运维th class container) and it's simply clones itself inside another div (with id containment-wrapper):

html

<span class="container">div one</span>

<div id="containment-wrapper">
</div>

Jquery

$(".container").dblclick(function() {

        $(this).clone().appendTo('#containment-wrapper');
});

When I double click the original div, it clones itself and puts a div inside the containment-wrapper but when I double click a cloned div, it doesn't do anything even though it has class=container, why is this happening? I tried many different ways to clone it but nothing worked.


It's because the event handler isn't cloned. Use delegate instead:

$(".container").delegate("","dblclick",function() {
  $(this).clone().appendTo('#containment-wrapper');
});


Because dblclick is not bounded to the new div. What you want to achieve can be easily done with live

$(".container").live('dblclick', function() {

        $(this).clone().appendTo('#containment-wrapper');
});


Since you're dynamically adding div tags they aren't automatically bound to the .dblclick function you've specified. Using a live event handler is one way to solve this.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜