mustache.js无法正常工作

时间:2015-05-20 10:59:35

标签: javascript mustache

我正在尝试学习胡子js,但基本页面不起作用。 我跟随http://iviewsource.com/codingtutorials/introduction-to-javascript-templating-with-mustache-js/,但它已经过时了。 (目前基本的html不起作用。将来我想在脚本中使用get JSON从json文件中获取数据。) 我错过了什么吗?

<!DOCTYPE <!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset="utf-8"/>
        <title>messages</title>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js">
        </script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/0.8.1/mustache.min.js">
        </script>
    </head>
    <body onload="loadUser()">
        <div id="target">Loading...</div>
        <script id="template" type="x-tmpl-mustache">
        Hello {{ subject }}!
        </script>
        <script>
            function loadUser() {
                var template = $('#template').html();
                Mustache.parse(template);   // optional, speeds up future uses
                var rendered = Mustache.render(template, {subject: "Luke"});
                $('#target').html(rendered);
            }
            });
        </script>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

语法错误。删除底部的额外括号(在下面的代码中注释掉)。

&#13;
&#13;
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset="utf-8"/>
        <title>messages</title>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js">
        </script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/0.8.1/mustache.min.js">
        </script>
    </head>
    <body onload="loadUser()">
        <div id="target">Loading...</div>
        <script id="template" type="x-tmpl-mustache">
        Hello {{ subject }}!
        </script>
        <script>
            function loadUser() {
                var template = $('#template').html();
                Mustache.parse(template);   // optional, speeds up future uses
                var rendered = Mustache.render(template, {subject: "Luke"});
                $('#target').html(rendered);
            }
            // }); <-- here
        </script>
    </body>
</html>
&#13;
&#13;
&#13;