2014-02-27 163 views
0

我已经克隆像jQuery的克隆元素

<div class = "button1"> Button1</div> 
<div class = "text1Container"> 
    <div class="myButton"></div> 
</div> 
<div class = "text2Container"></div> 

这里元素我的jQuery

$(".button1").click(function() { 
    $(".text1Container").clone().appendTo(".text2Container"); 
}); 

所以现在我想在“text2Container” 这样一套点击功能,但它没有工作

//why not working 
$(".text2Container .myButton").click(function() { 
    alert('hello'); 
}); 

回答

3

您应该使用.on()为动态创建的html元素绑定事件。试试这个:

$(".text2Container").on('click','.myButton',function() { 
    alert('hello'); 
}); 

而你在你的代码中有错字。与.button1更换button1

$(".button1").click(function() { 
    $(".text1Container").clone().appendTo(".text2Container"); 
}); 

DEMO

+0

不工作,同样的问题 –

+0

我试图与这.button1也没有工作 –

+0

@UsmanAhmad:检查演示链接。 – Unknown

0

你实际上并没有分配点击您button1。你缺少.类选择:

$(".button1").click(function() { 
    $(".text1Container").clone().appendTo(".text2Container"); 

    $(".text2Container .myButton").click(function() { 
     alert('hello'); 
    }); 
}); 

注意,.myButton绑定代码已与.button1 click处理程序是英寸您不能将事件绑定到尚不存在的元素。

Fiddle