使用" Mustache.js"渲染样本数据没有显示

时间:2015-08-10 10:48:06

标签: javascript jquery templates mustache

我有一个简单的代码,其中模板由Mustache.js完成,但无法在屏幕上显示任何内容。控制台中也没有错误。请帮助!

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/2.1.3/mustache.js" ></script>
    <script>
        $(document).ready(function(){
            var person = {
            firstName: "Christophe",
            lastName: "Coenraets",
            blogURL: "http://coenraets.org"
            };
            var template = "<h1>{{firstName}} {{lastName}}</h1>Blog: {{blogURL}}";
            var html = Mustache.to_html(template, person);
            $('#div1').html(html);
        });
    </script>
</head>
<body>
<div id="#div1"></div>
</body> 
</html>

任何人都可以告诉问题在哪里?

2 个答案:

答案 0 :(得分:3)

将div标记

中id属性的'#div1'替换为'div1'

答案 1 :(得分:1)

http://plnkr.co/edit/m14fkua89UOAjlX506U5?p=preview     只需将#div1替换为div

 <html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/2.1.3/mustache.js" ></script>
           <link rel="stylesheet" href="style.css">
        <script>
            $(document).ready(function(){
                var person = {
                firstName: "Christophe",
                lastName: "Coenraets",
                blogURL: "http://coenraets.org"
                };
                var template = "<h1>{{firstName}} {{lastName}}</h1>Blog: {{blogURL}}";
                alert(template);
                var html = Mustache.to_html(template, person);
                alert(html);
                $('#div1').html(html);
            });
        </script>
    </head>
    <body>
    <div id="div1"></div>
    </body> 
    </html>