jquery - click event does not works with copied HTML -
jquery - click event does not works with copied HTML -
this question has reply here:
event binding on dynamically created elements? 13 answersclick event not works copied html?
<ul class="dir">//the given ul <li> <img class="thumb" src="1.jpg" > </li> <li> <img class="thumb" src="2.jpg" > </li> <li> <img class="thumb" src="3.jpg" > </li> <li> <img class="thumb" src="4.jpg" > </li> <li> <img class="thumb" src="5.jpg" > </li> <li> <img class="thumb" src="6.jpg" > </li> <li> <img class="thumb" src="7.jpg" > </li> </ul> <ul class="copy"></ul> //copied ul <script> $(function(){ $(".copy").html($(".dir").html()); $(".thumb").click(function(){ alert("it works on .dir's thumb not on .copy's thumb .. why?"); } ); } </script>
you have utilize event-delegation
$(document).on('click', '.thumb', function(){ alert("this works every if copied"); });
for work. .click
attach handler elements, existed @ point of declaring handler.
jquery html
Comments
Post a Comment