简体   繁体   English

当动态创建具有某个类的元素时,如何引发jquery事件

[英]How to raise a jquery event when an element with some class is created dynamically

Is it possible to raise a jquery event when an element with a certain class is created dynamically? 当动态创建具有特定类的元素时,是否可以引发jquery事件? This is what I mean. 这就是我的意思。 I have the following; 我有以下几点;

...
<div id="lotoContent"></div>
...

Using jquery ajax, I'm retrieving several rows from the server and appending them to the "lotoContent" div. 使用jquery ajax,我从服务器检索几行并将它们附加到“lotoContent”div。 Each row has an empty span element that looks like this 每行都有一个空的span元素,如下所示

<span class="lotoDateTime" data-date="123456"></span>

The value of the data-date attribute is a unix time-stamp retrived from the database. data-date属性的值是从数据库中恢复的unix时间戳。 Once the rows are added, the following javascript function is called; 添加行后,将调用以下javascript函数;

function processDateTimes() {
   //process dates
    $('.lotoDateTime').each(function () {
        var timeStamp = $(this).data('date');
        $(this).text(getDateTimeFromServerUnixTimeStamp(timeStamp));
    });

}

function getDateTimeFromServerUnixTimeStamp(timeStamp) {
  //this function takes the unix time-stamp and converts it to Date and Time 
  // like 08/09/2013 12:09 based on the browser time
}

This works fine, but I was wondering if there is a way to automatically call processDateTimes() when the date spans are created instead of manually calling the function after they are created. 这工作正常,但我想知道是否有一种方法可以在创建日期跨度时自动调用processDateTimes(),而不是在创建日期后手动调用该函数。 Something like this is what I have in mind; 这样的事情就是我想到的;

$('#lotoContent').on('SomeEvent', '.lotoDateTime', function() {
  processDateTimes();
});

Thanks. 谢谢。

The word you may be looking for is observables . 你可能正在寻找的词是可观察的 In essence, when the DOM (or in this situation a span element) is updated you'd like to trigger an event. 实质上,当DOM(或在这种情况下是span元素)更新时,您想要触发事件。

For that answer I'd like to direct your attention to this response, 对于那个答案,我想引导你注意这个回答,

https://stackoverflow.com/a/240663/191006 https://stackoverflow.com/a/240663/191006

Ken illustrates that capturing all DOM changes at the top will allow you to pick and choose what do to on certain inner elements. Ken表明,捕获顶部的所有DOM更改将允许您选择对某些内部元素执行的操作。

$('body').change(function(event){

    if( $(event.target).hasClass("lotoDateTime") )
        processDateTimes();

 });

You could certainly clean this up... you don't need to check for the class like I am... but I hope that helps you get going in the right direction. 你当然可以清理它......你不需要像我一样检查课程......但我希望这能帮助你朝着正确的方向前进。

can you not give the new rows a class of "new" when the ajax populates them and change the function processDateTimes() to run on items with the class of "new" and then remove the class once it's done its thing. 当ajax填充它们并更改函数processDateTimes()以在类“new”的项目上运行时,你能否给新行一个“new”类,然后在它完成它之后删除它。

finally invoke the function at the end of your ajax call (complete) ? 最后在你的ajax调用结束时调用函数(完成)?

$.ajax( ... ,
  success: function() {
    // add your element and give it a class of "new"
  },
  complete: function() {
    // then run your process function:
    processDateTimes();
  }
);

// this will only run on "new" items added
function processDateTimes() {

  $('.lotoDateTime.new').each(function() {
    var timeStamp = $(this).data('date');
    $(this)
      .text(getDateTimeFromServerUnixTimeStamp(timeStamp))
      .removeClass('new');

  });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM