javascript 未捕获的类型错误:无法调用 null 的方法“替换”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10659592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught TypeError: Cannot call method 'replace' of null
提问by sparkle
If I type "_.template($('#pranks-list').html())" on Chrome JS console it's works as well
如果我在 Chrome JS 控制台上输入“_.template($('#pranks-list').html())”,它也能正常工作
>> _.template($('#pranks-list').html())
function (a){return e.call(this,a,b)}
app.js // Views
app.js // 视图
window.PranksListView = Backbone.View.extend({
template: _.template($('#pranks-list').html())
});
Index.html
索引.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())
回答by u.k
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())
在创建 dom 并且节点在那里之前运行。通常,当您准备好模板变量时,在渲染时渲染模板是一个好习惯:
_.template($('#pranks-list').html(), {id: 'foo', name: 'bar'});