javascript - 未捕获的类型错误 : Cannot read property 'replace' of undefined -underscore

标签 javascript jquery underscore.js

我是 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>

它显示一个错误:未捕获类型错误:无法读取未定义的属性“替换” 我使用括号编辑器。如果有人能告诉我,我将非常感激 1)这是什么错误? 2)我哪里出错了? 。提前非常感谢。

最佳答案

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

关于javascript - 未捕获的类型错误 : Cannot read property 'replace' of undefined -underscore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34714067/

相关文章:

javascript - 如何养育 child ul?

javascript - 当用户键入或进行选择时启用搜索按钮

jquery datepicker 删除 .ui-state-highlight 类 onSelect 事件

javascript - 使用jquery动态获取名称属性

javascript - 比较 2 个数组以查看对象是否存在

coffeescript - 在 CoffeeScript 中使用 Underscore.js

javascript - 返回带有过滤子数组的父对象

javascript - 将 div 放置在文本区域 jquery 中的文本下方

javascript - 添加行然后删除会导致重复行

javascript - MUI + React Hook 表单 : Fill out TextField value but then can't modify the value