【问题标题】:Uncaught TypeError: Cannot read property 'replace' of undefined -underscore未捕获的类型错误:无法读取未定义的属性“替换”-下划线
【发布时间】:2016-01-11 04:09:14
【问题描述】:

我是 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)我哪里出错了?? .非常感谢。

【问题讨论】:

    标签: javascript jquery underscore.js


    【解决方案1】:

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

    【讨论】:

    • 现在它抛出此错误:资源解释为脚本,但使用 MIME 类型 application/x-js 传输:“file:///E:/​​its%20me/softies/blackhole.com/underscore. js”在第 4 行(我从 github 放置下划线 lib 函数的行)
    • 这应该是警告,而不是错误吧?它是由您从文件系统而不是适当的网络服务器提供服务引起的。尝试使用 XAMPP 之类的东西进行开发。否则,请打开一个新问题。
    猜你喜欢
    • 2021-11-03
    • 2014-06-09
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    • 1970-01-01
    • 2021-12-22
    相关资源
    最近更新 更多