0

我正在尝试使用 Tempo.js 进行模板化,文档稀疏。请尝试在 SO 中进行,而不是在 jsFiddle 中进行。

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>

            <script type="text/javascript" src="https://raw.github.com/twigkit/tempo/master/tempo.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>


        <script type="text/javascript" charset="utf-8">

            var data = [
                    {'name':{'first':'Leonard','last':'Marx'},'nickname':'Chico','born':'March 21, 1887','actor': true,'solo_endeavours':[{'title':'Papa Romani'}]},
                    {'name':{'first':'Adolph','last':'Marx'},'nickname':'Harpo','born':'November 23, 1888','actor':true,'solo_endeavours':[{'title':'Too Many Kisses','rating':'favourite'},{'title':'Stage Door Canteen'}]},
                    {'name':{'first':'Julius Henry','last':'Marx'},'nickname':'Groucho','born': 'October 2, 1890','actor':true,'solo_endeavours':[{'title':'Copacabana'},{'title':'Mr. Music','rating':'favourite'},{'title':'Double Dynamite'}]},
                    {'name':{'first':'Milton','last':'Marx'},'nickname':'Gummo','born':'October 23, 1892'},
                    {'name':{'first':'Herbert','last':'Marx'},'nickname':'Zeppo','born':'February 25, 1901','actor':true,'solo_endeavours':[{'title':'A Kiss in the Dark'}]}
                ];

            Tempo.prepare('marx-brothers').render(data);

        </script>       

    </head>
    <body>

    <ol id="marx-brothers">
        <li data-template>{{nickname}} {{name.last}}</li>
    </ol>

    </body>
</html>
4

1 回答 1

2

您试图在Tempo.prepare具有它引用的 ID 的 DOM 加载之前运行调用。

要么将脚本标签放在</body>结束标签之前,要么等到 DOM 准备好后再运行代码,使用某些东西window.onload或 jQuery 的$(document).ready(...)模式。

于 2013-02-13T23:58:46.867 回答