(이)작동하지 않;결과에"catch 되지 않는 형식 오류:할 수 없는 읽기 속성'문'undefined"

StackOverflow https://stackoverflow.com//questions/23054906

문제

나는 다음과 같은 코드를 Chrome Inspector 디버깅 오류"catch 되지 않는 형식 오류:할 수 없는 읽기 속성'문'undefined":

<script type="text/javascript">
  $(function countComments() {
    var mcount = '//api.moot.it/postcounts?path=/forum/comments:' + $(this).closest('article').attr('id').substring(8);
    $.getJSON(mcount, function(json) {
      var results = $('.entry-actions');
      $.each(json, function(key, val) {
        results.prepend('<a class="entry-comments" href="{permalink}#comments" title="Comments">' + val['size'] + ' Comments</a>');
      });
    });
  });
</script>

나는 확 문제가 있는 것입 (this), 지만,나는 그것이 제대로에서 서로 다른 스크립트로 .click 그는 코드의 잘 작동합니다.

기 때문에 그것은 (this) 에 사용할 수 없 .ready?그렇다면,어떻게 이 기능 이 작동하려면 대체 코드?

HTML 편집:

<article id="article-5344bff8e4b01730378236ff">
  <header class="entry-header cf">
    <p class="entry-actions"></p>
  </header>
    <div class="entry-title-wrapper">

    BLOG CONTENTS

    <div class="entry-injection">
    <script type="text/javascript">
      $(function countComments() {
        var mcount = '//api.moot.it/postcounts?path=/forum/comments:' + $(this).closest('article').attr('id').substring(8);
        $.getJSON(mcount, function(json) {
          var results = $('.entry-actions');
          $.each(json, function(key, val) {
            results.prepend('<a class="entry-comments" href="{permalink}#comments" title="Comments">' + val['size'] + ' Comments</a>');
          });
        });
      });
    </script>
    </div>
    </div>
  </article>
도움이 되었습니까?

해결책

문제는 문서 준비 기능 this 포인트 문서 http://jsfiddle.net/IrvinDominin/J2r5V/

이 경우에는 사용할 수 없 article 부모는 요소의 문서,그래서 당신의 기능을 높이는 오류 http://jsfiddle.net/IrvinDominin/J2r5V/1/.

내가 생각하는 당신은 모든 div 요소에 따라 필요에,그리고 그들 각각의 실행하여 현재의 함수의 컨텍스트에서 각각 this 될 것입니다 정확한 요소입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top