2016-06-17 5 views
0

とヌルである私は、jQueryの2.2.4と小さなスクリプトを使用していますが、スクリプトはjQueryのは私に次のエラーを与える行っている:jQueryのは、TypeError例外を取得:aはコード

コンソールがa.ownerDocument」を指している
TypeError: a is null 


...th>0},fa.contains=function(a,b){return(a.ownerDocument||a)!==n&&m(a),t(a,b)},fa.... 

は今、このエラーが起こる作るための責任のコードです:

$(function() { 
     $('[class*="inc:"]').each(function() { 
      var match = /inc:(\S+)/.exec(this.className || ''); 
      match && $(this).inc(unescape(match[1])); 
     }); 
    }); 

私のindex.htmlコードはこれですここ

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <title></title> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> 
    <script type="text/javascript" src="inc.js"></script> 

</head> 
<body> 

    <p class="inc:footer.html">This text will be replaced 
    with footer.html</p> 


</body> 
</html> 

がいっぱいinc.jsです:

(function($) { 
    $.fn.inc = function(url, transform, post, t) { 
     return this.length && url ? this.each(function() { 
      t = $(this); 
      $.ajax({ 
       url: url, 
       success: function(txt, jqXHR, textStatus) { 
        t.html($.isFunction(transform) ? transform(txt, url) : txt); 
        $.isFunction(post) && post(url, jqXHR, textStatus); 
       } 
      }); 
     }) : this; 
    }; 

    $(function() { 
     $('[class*="inc:"]').each(function() { 
      var match = /inc:(\S+)/.exec(this.className || ''); 
      match && $(this).inc(unescape(match[1])); 
     }); 
    }); 

})(jQuery); 

は、どのように私はこの問題を解決することができますか?

+2

これは 'class'属性の暴力的な虐待です。 CSSセレクタでなければ 'class'を使わないでください!代わりに、もっと論理的なものを使用してください。 '

' jQueryは '$( '[data-include]')でこれにアクセスできます。 ); ' –

+0

@RoryMcCrossan McCrossan、inc.js全体を質問に追加しました。 –

答えて

2

利用代わり

$(function() { 
    $('[class*="inc:"]').each(function() { 
     var match = /inc:(\S+)/.exec(this.className || ''); 
     if(typeOf(match[1]) != "undefined") 
      match && $(this).inc(unescape(match[1])); 
    }); 
    }); 
関連する問題