未捕获类型错误:无法调用空的“替换” [英] Uncaught TypeError: Cannot call method 'replace' of null

查看:126
本文介绍了未捕获类型错误:无法调用空的“替换”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我输入_.template($('#恶作剧名单)。HTML())铬JS控制台上它的工作原理,以及

 >> _.template($('#恶作剧名单)。HTML())
功能(一){返回e.call(此,A,B)}

app.js
    //浏览

  window.PranksListView = Backbone.View.extend({    模板:_.template($('#恶作剧名单)HTML())
});

的index.html
      
        
        
        结果
        

 <脚本类型=text / html的ID =恶作剧名单>
    <李>< A HREF ='#恶作剧/<%= ID%GT;'><%=名称%GT;< / A>< /李>
  < / SCRIPT>  < /身体GT;

为什么我得到这条线?这个错误

 模板:_.template($('#恶作剧名单)HTML())


解决方案

这很难说没有看到整个code,但你可能尝试运行 _。模板($(' #恶作剧名单)。HTML())创建之前DOM和节点是存在的。
通常它是一个很好的做法,在渲染渲染时间,当你有模板变量模板准备:

  _模板($('#恶作剧名单)HTML(){ID:'富',名称:'酒吧'});

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

解决方案

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

这篇关于未捕获类型错误:无法调用空的“替换”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆