我正在使用插件 jquery-tmpl .有没有办法在模板运行后指定回调?我想做类似的事情
<script id='itemTemplate' type='text/html'>
<li class="item" id=${timestampMs}>
<span class="content">${content}</span>
</li>
${processItem($('#' + timestampMs))}
</script>
在哪里processItem
对 <li>
做些什么刚刚生成的元素。但是,正如所写的那样,该元素当时不存在 processItem
被称为。
这是我运行模板的方式:
// Make the AJAX call to the service
$.ajax({
dataType: "json",
url: "/getItems",
success: function(data) {
// fill out template from json
$('#itemTemplate').tmpl(data).appendTo('#container');
}
});
谢谢!
最佳答案
调用 .tmpl 后,您就有了可以操作的节点,因此您可以这样做:
$.ajax({
dataType: "json",
url: "/getItems",
success: function(data) {
// fill out template from json
$('#itemTemplate').tmpl(data).each(function (index) {
processItem($(this))
}).appendTo('#container');
}
});
这与您的解决方法类似,但您不需要重新选择项目,并且您应该能够链接调用。
关于javascript - jquery-templ 的回调函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4029504/