Uncaught TypeError: Cannot read property 'replace' of undefined -underscore

Uncaught TypeError: Cannot read property 'replace' of undefined -underscore

我是 underscore.js 和 jquery 的初学者,当我创建一个简单的模板时,如下所示:

<html>
<body>

    <script src="E:/its me/softies/blackhole.com/underscore.js" type="text/javascript"></script>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>


    <script type ="text/javascript" id="tem">
        var head = "hii";
        //var head2 = "hello";
        var templ = _.template($('#temp').html()) ;
        $('#tagent').append(templ,{head:head});        
    </script>

      <script type="text/template" id="temp">
    <h1><%=head></h1>
    <h2><%=head2></h2>
    </script>

    <div class="tangent"></div>
</body>

它显示错误:Uncaught TypeError: Cannot read property 'replace' of undefined 我使用括号编辑器。如果有人能告诉我,我将不胜感激 1)这是什么错误?? 2)我哪里出错了?? .非常感谢。

您的脚本在您的模板可用之前执行,您需要在 document.ready 上执行它或将您的脚本块移动到您的模板定义下方。