最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: Cannot call method 'replace' of null - Stack Overflow

programmeradmin9浏览0评论

If I type "_.template($('#pranks-list').html())" on Chrome JS console it's works as well

>> _.template($('#pranks-list').html())
function (a){return e.call(this,a,b)}

app.js // Views

window.PranksListView = Backbone.View.extend({

    template: _.template($('#pranks-list').html())
});

Index.html

  <script type="text/html" id="pranks-list">
    <li><a href='#pranks/<%= id %>'><%= name %></a></li>
  </script>

  </body>

Why I get this error on this line??

template: _.template($('#pranks-list').html())

If I type "_.template($('#pranks-list').html())" on Chrome JS console it's works as well

>> _.template($('#pranks-list').html())
function (a){return e.call(this,a,b)}

app.js // Views

window.PranksListView = Backbone.View.extend({

    template: _.template($('#pranks-list').html())
});

Index.html

  <script type="text/html" id="pranks-list">
    <li><a href='#pranks/<%= id %>'><%= name %></a></li>
  </script>

  </body>

Why I get this error on this line??

template: _.template($('#pranks-list').html())
Share Improve this question asked May 18, 2012 at 20:52 sparklesparkle 7,39824 gold badges76 silver badges146 bronze badges 3
  • just search for replace in those scripts, you can see which variable is being null, or put some code here – Ranganadh Paramkusam Commented May 18, 2012 at 20:55
  • I have never seen html in <script> tags before. Are you sure you don't want a div instead? – Ash Burlaczenko Commented May 18, 2012 at 20:59
  • 1 @AshBurlaczenko: It's a pretty mon way to store templates. With a type that will not cause some scripting engine to execute it it's a great way and much cleaner than invisible divs. – ThiefMaster Commented May 18, 2012 at 21:01
Add a ment  | 

1 Answer 1

Reset to default 15

It's hard to tell without seeing the whole code, but you are probably trying to run _.template($('#pranks-list').html()) before the dom is created and the node is there. Usually its a good practice to render the template on render time when you have the template variables ready:

_.template($('#pranks-list').html(), {id: 'foo', name: 'bar'});
发布评论

评论列表(0)

  1. 暂无评论