Uncaught TypeError: Cannot read property ‘replace‘ of undefined -underscore.js

  • 时隔3年,又恰逢其会,拾起了前端代码的开发工作,一头扎进去,才发现,前端技术已经物是人非了,因此记录下遇到的自认为有意义的问题。

问题示例:

<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>
问题答案:

Your script is executed before your template is available, you need to
ether execute it on document.ready or move your script block below
your template definition.

  • 感觉说的很准确,一语中的,很赞!!!

解决方法参考:Stack Overflow

你可能感兴趣的:(前端技术)