未捕获的TypeError:无法调用方法' replace'的null [英] Uncaught TypeError: Cannot call method 'replace' of null
问题描述
如果我在Chrome JS控制台上输入"_.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 //观看次数
app.js // Views
window.PranksListView = Backbone.View.extend({
template: _.template($('#pranks-list').html())
});
Index.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())
推荐答案
很难看到完整的代码,但是您可能正在尝试在创建dom和节点存在之前运行_.template($('#pranks-list').html())
.
通常,在准备好模板变量后,最好在渲染时渲染模板:
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'});
这篇关于未捕获的TypeError:无法调用方法&#39; replace&#39;的null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!