Chrome Inspector がエラー "Uncaught TypeError: Cannot read property 'substring' of 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>