开发者

onclick handler doesnt work after jquery AJAX html injection

I am injecting some HTML with ajax into my page but the onclick handler on a part of that injected HTML doesn't seem to work...

basically this image...

< img class="close_row" src="img/close.gif"/>

my jquery goes like...

$开发者_JS百科('.close_row').click(function() {
    // some code to close the row
});

the original function to open the row and inject the html works fine, am i overlooking something here?


Use .delegate() instead.

delegate() was introduced to overcome live()'s shortcomings and live() would have been completely removed if it wasn't for the fact that it would have broken a lot of code.

Differences between live() and delegate(): http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-the-difference-between-live-and-delegate/

API: http://api.jquery.com/delegate/


You'd have to use .live:

$('.close_row').live('click', function() { // some code to close the row });

.click binds the event to all current elements, whereas .live will automatically bind the event to newly created elements.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜