【发布时间】:2011-06-16 10:46:39
【问题描述】:
我希望能够创建由一些异步查询获取的数据填充的 jQuery 模板,为此我需要参考我在模板中呈现的元素。我想出了一个使用生成的类名的解决方案,我在渲染模板后使用它来插入数据,但这是一个杂项。有没有办法从模板代码中获取对渲染元素的引用?这是我想要工作的代码:
<script>
function renderData(prduct_id, element){
getProductData(product_id, function(data){
$(e).empty();
$("#product-info-tmpl").tmpl({"data":data}).appendTo($(element));
});
}
</script>
<script id="product-info-loading-tmpl" type="text/x-jquery-tmpl">
loading
//Here is where I'd need a reference to the rendered element
${renderData(product_id, this_element_when_rendered)}
</script>
【问题讨论】:
-
当更多地考虑这一点时,可能无法获得对模板在编译时将呈现的内容的引用,因此需要一种不同的方法。我正在使用 html5 数据属性重做它,但如果有人对此有更多意见,请继续发布答案。
标签: javascript jquery ajax jquery-templates