开发者

Make jQuery works for element created with "insertAfter" etc

开发者 https://www.devze.com 2023-01-06 12:07 出处:网络
I have some elements like <a class=\'link\'>click</a> which are created after clicking on another div ...

I have some elements like <a class='link'>click</a> which are created after clicking on another div ...

Is there any chance to make jQuery works for <a class='link'>click</a> ?

For now I'm forced to use <a onclick='func()' class='link'>cl开发者_StackOverflow社区ick</a> ... and it's not possible to make something like alert($(this).html()) (which must pop-up the text "click").

Thanks


You could use jQuery's live() method which will handle events for dynamically added elements.

$('a.link').live('click', function() {
    alert( $(this).html() );
});
  • http://api.jquery.com/live/

Or you could just bind the click like normal when you create them.

$('<a class="link">click</a>').click(function() {
    alert( $(this).html() );
}).insertAfter(someselector);
0

精彩评论

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