zxw394
2014-05-19 21:37
浏览 359
已采纳

使用js模板handlebars时html中代码报错

小弟刚在学handlebars遇到在body标签中写入带有{{name}}代码时 报错如图:

 

 

刚学真是不知道是什么原因,希望哥哥姐姐们帮忙看下!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • crespo1985ban 2014-05-19 21:58
    已采纳

    题主啊,你看你的最后一行:
    $(".mainBody").html(html);

    页面中根本就没有这个元素啊。

    附件是我改好的代码,你看看吧。[code="html"]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">


    handlebarExample.html


    <br> $(document).ready(function(){<br> var source = $(&quot;#entry-template&quot;).html();<br> var template = Handlebars.compile(source);<br> var context = {title: &quot;标题&quot;, body: &quot;内容&quot;}<br> var html = template(context);<br> $(&quot;.mainBody&quot;).html(html);<br> });<br>

    <div class="entry"> <h1>{{title}}</h1> <div class="body"> {{body}} </div> </div>


    [/code]

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • iteye_11009 2014-05-19 21:55

    <!DOCTYPE html>


    Handlebars Expressions Example



    Handlebars Expressions Example!


    <!--this is a list which will rendered by handlebars template. -->

        <script type="text/javascript" src='#'" /jquery.js">
    


    <br>

        <script id="people-template" type="text/x-handlebars-template">
    

    {{#people}}


    {{first_name}} {{last_name}}


    {{phone}}


    User since {{member_since}}


    {{/people}}
        <script type="text/javascript">
    

    $(document).ready(function() {

    // compile our template
        var template = Handlebars.compile($("#people-template").html());
    
        var data = {
            people : [ {
                first_name : "rui",
                last_name : "fengyun",
                phone : "1234567890",
                email : "alan@test.com",
    
                member_since : "Mar 25, 2011"
            }, {
                first_name : "Allison",
                last_name : "House",
                phone : "0987654321",
                email : "allison@test.com",
    
                member_since : "Jan 13, 2011"
            }, {
                first_name : "Nick",
                last_name : "Pettit",
                phone : "9836592272",
                email : "nick@test.com",
    
                member_since : "Apr 9, 2009"
            }, {
                first_name : "Jim",
                last_name : "Hoskins",
                phone : "7284927150",
                email : "jim@test.com",
    
                member_since : "May 21, 2010"
            }, {
                first_name : "Ryan",
                last_name : "Carson",
                phone : "8263729224",
                email : "ryan@test.com",
    
                member_since : "Nov 1, 2008"
            } ]
        };
    
        $('#list').html(template(data));
    });
    



    感觉你那写法有点 按照这个参考 重新试试

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题