Making a jQuery Plugin Work On a dynamically added element
Well, I've got this plugin (http://timeago.yarp.com/) which works ok on existing element. However, when I dynamically add an element, the effect isn't applied on that new elem开发者_JAVA技巧ent. How can I make the plugin work on dynamically added elements?
the syntax for the plugin is:
$("abbr.timeago").timeago();
this is a known problem when adding dynamic elements. You need to rebind the element you've just added.
So for instance:
$.ajax({
url: "example.html",
cache: false,
success: function(html){
var $jqueryElement = $(html);
$("abbr.timeago", $jqueryElement).timeago();
$("#results").append($jqueryElement);
}
});
The example above rebinds abbr.timeago returned from the example.html and adds them to the DOM.
not sure but you can try something like this
$("abbr.timeago").ready(function(){
$(this).timeago();
});
Each time your JS adds a new element, just call .timeago()
on it.
精彩评论