javascript - 访问加载在 html head 标签中的 mustache 模板?

标签 javascript mustache

有没有办法访问通过 HTML 加载的 mustache 模板文件的内容 <head>通过 javascript?

<link rel="template" href="templates/address.mustache" type="text/html" />

<script src="templates/address_field.mustache" type="text/html" charset="utf-8" id="address_template"></script>

我已经成功地通过 ajax 和 <script> 加载了它们标记在主体中,但我不确定如何通过头部作为单独的文件加载时获取文件源。

最佳答案

您需要获取方法来捕获外部模板的模板值:以下代码仅在 html 页面内有效:

var tmpl=$.trim($('#address_template').val()); //trim the white spaces in the template

Mustache.to_html(tmpl,json);

你需要下面的代码:“$.get('template.html',”是你的答案技巧。你应该首先加载你的模板:

<!DOCTYPE html>

标题

<script>
    $(function() {
        $.getJSON('/data/speakers.json', function(data) {
            //var template = $('#speakers-template').html();

           // console.log(template);
           // var info = Mustache.render(template, data);
           // $('#talktitles').html(info);

$.get('template.html', function(template, textStatus, jqXhr) { var info =Mustache.render($(template).filter('#speakers-template').html(), data); $('#talktitles').html(信息); });

        });
    });
</script>

关于javascript - 访问加载在 html head 标签中的 mustache 模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5722521/

相关文章:

javascript - 如何使用 Handlebars 渲染斜体/粗体文本?

javascript - Mustache 平面数组到嵌套元素中

php - 如何存储音频文件的缓存值?

javascript - 我可以触发 xe :namepicker via csjs?

javascript - d3.select 不更新网页中的图表

javascript - 由于缩小而替代 eval() 函数

javascript - 在 Node 上要求与浏览器上的 mustache 模板相同

javascript - 在 mustache 模板中包含 javascript

javascript - 如何使用 jQuery 访问通过 Mustache 渲染到页面中的元素

javascript - window.close() 不工作(最初通过 JS/JQuery 打开)