Uncaught TypeError: Cannot call method 'replace' of null

不打扰是莪最后的温柔 提交于 2019-12-07 06:30:33

问题


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())

回答1:


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'});


来源:https://stackoverflow.com/questions/10659592/uncaught-typeerror-cannot-call-method-replace-of-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!