动态创建具有特定类的元素时是否可以引发 jquery 事件?这就是我的意思。我有以下内容;
...
<div id="lotoContent"></div>
...
使用 jquery ajax,我从服务器检索几行并将它们附加到“lotoContent”div。每行都有一个空的 span 元素,看起来像这样
<span class="lotoDateTime" data-date="123456"></span>
data-date 属性的值是从数据库中检索的 unix 时间戳。添加行后,将调用以下 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
}
这很好用,但我想知道是否有一种方法可以在创建日期跨度时自动调用 processDateTimes() 而不是在创建日期后手动调用该函数。我的想法是这样的;
$('#lotoContent').on('SomeEvent', '.lotoDateTime', function() {
processDateTimes();
});
谢谢。