javascript - 异步 jQuery 模板

标签 javascript jquery ajax jquery-templates

我希望能够创建由一些异步查询获取的数据填充的 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>

最佳答案

这有帮助吗,所以在你的情况下,有 $item 的东西,也许 $item.parent 或其他东西......

http://api.jquery.com/template-tag-equal/ 的某处复制

The $item and $data Template Variables The following variables are exposed to expression evaluation within templates:

$: The jQuery object. $item: The current template item - which allows access to $item.data, $item.parent, etc. as well as any user-defined values or methods passed in with the options map. $data: The current data item (equivalent to $item.data). Note: A template tag with content such as {{each}}...{{/each}} may expose additional variables to template evaluation within the content. In the case of {{each}}, for example, the additional template variables $value and $index are provided within the content of the {{each}} tag.

关于javascript - 异步 jQuery 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4815057/

相关文章:

javascript - CSV 导入脚本在 Google 电子表格中不起作用

jQuery 循环和重置自定义 css 动画

javascript - CFM 文件中的 jQuery AJAX 和函数

javascript - XMLHttpRequest 仅适用于 IE

javascript - 将标记从国家名称放入国家

javascript - MPEG-DASH 跨浏览器播放

jquery - 针对列表中的特定类

javascript - 杰森响应。使用 javascript 或 php 检索特定值

ajax - Chrome 无法 POST 到 https,但 GET 使用自签名的 SSL 证书?

javascript - Material UI - 错误 : Invalid hook call using create-react-library + MUI