开发者

Binding event to DOM elements which can be dynamically loaded

In my page, I'll be having few DIVs with class name 'imp'. And I might also be loading a few more DIVs with same class name. I want to bind all these DIVs with an event (may be 'load') so that I can meet the requirement of executing a function for all these DIVs irrespective of whether they are already present on 开发者_如何学Pythonthe page or will be loaded later. I have tried the following but didn't have any success.

$('.imp').live('load', function()
{
  alert('encountered an important data');
});

The problem here is that 'load' event can be bound only to the element with url like images, scripts etc. Kindly let me know how can I achieve the above mentioned requirement.


In your jQery Ajax call (like load), you can specify a function which will run when the call is completed and successful. Might run your function there.

Something like:

$('#result').load('ajax/test.html', function() {
  $('.imp', $('#result')).each(function (){
    alert('encountered an important data');
  });
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜