【问题标题】:jQuery - Error in event handler for 'undefined': TypeError: Cannot call method 'replace' of undefinedjQuery - “未定义”的事件处理程序中的错误:TypeError:无法调用未定义的方法“替换”
【发布时间】:2011-09-30 09:29:55
【问题描述】:

我有自己的模板,头部有这段代码:

  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"></script>
  <script type="text/javascript" src="{{MEDIA_URL}}emotions.js"></script>

但在 Chrome 代码光谱仪中,我总是有:

Error in event handler for 'undefined': TypeError: Cannot call method 'replace' of undefined

emotions.js:

$(function(){
        $('.emotion').click(function(){
                  var id = $(this).parent().attr('id')
                  $.post("something/vote/", { emotion: "emotion", id: "id" } )
        $('.vote_value').text('You give your vote')});
});

有什么可能出错的建议吗?

【问题讨论】:

  • 10:1 故障位于emotions.js
  • 会显示什么{{MEDIA_URL}}..?
  • {{MEDIA_URL}} 给出的值是文件emotion.js的url路径
  • 您是否尝试过回显 {{MEDIA_URL}}..?可能只需要 {MEDIA_URL}..
  • 不,我肯定需要 {{MEDIA_URL}} 并且肯定写得很好。它是 django 特性,上下文变量。

标签: jquery django google-chrome


【解决方案1】:

我也有这个错误。我刚刚关闭了“adBlock”扩展。然后我重新启动了我的服务器并且错误消失了。

【讨论】:

    【解决方案2】:

    我几乎可以保证,如果您登录 $('.vote_value'),您将得到未定义。确保 dom 中存在具有该类的元素。

    【讨论】:

      【解决方案3】:

      如果您请求 html()&lt;title&gt; 元素,也可能会收到此错误。例如:

      var title = $('title');
      var t = title.html();
      //-> Error in event handler for 'undefined': TypeError: Cannot call method 'replace' of undefined
      var t = title[0].childNodes[0].nodeValue;
      //-> "Welcome to My Page!"
      

      【讨论】:

        猜你喜欢
        • 2014-09-18
        • 1970-01-01
        • 1970-01-01
        • 2014-09-18
        • 1970-01-01
        • 2013-01-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多