使一个jQuery插件工作在一个动态添加的元素上 [英] Making a jQuery Plugin Work On a dynamically added element

查看:288
本文介绍了使一个jQuery插件工作在一个动态添加的元素上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有这个插件(http://timeago.yarp.com/),它可以在现有元素上正常工作。但是,当我动态添加一个元素时,该效果不适用于该新元素。如何使插件在动态添加的元素上工作?



插件的语法是:



<$ p $ 。p> $( abbr.timeago)TIMEAGO();


解决方案

添加动态元素时,这是一个已知问题。您需要重新绑定刚刚添加的元素。



例如:

 < code $ $ ajax({
url:example.html,
cache:false,
success:function(html){
var $ jqueryElement = $ (html);
$(abbr.timeago,$ jqueryElement).timeago();
$(#results)。append($ jqueryElement);
}
});

上面的示例重新绑定了从example.html返回的abbr.timeago,并将它们添加到DOM中。 / p>

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 element. 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.

这篇关于使一个jQuery插件工作在一个动态添加的元素上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆