(これは)うまくいきません。「キャッチされていないTypeError:未定義のサブストリング」を読み取ることができません」

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

質問

次のコードは、エラーが発生しているChrome Inspectorがデバッグするコードがデバッグされています。

<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がドキュメントを指しているということです

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top