2017-05-30 29 views
0

これは簡単なはずですが、動作させることができないようです。<th>タグのテキストを取得する方法

HTML

<table id="mytable"> 
    <thead> 
     <tr> 
     <th>Head1</th> 
     <th>Head2</th> 
     </tr> 
    </thead> 
    <tbody> 
     ..body stuff.. 
    </tbody> 
</table> 

JS

$("#mytable > thead > tr > th").each(function() { 
    console.log($(this).text(); 
}) 

コンソールには、各テキスト値の "未定義" を示しています。

+1

jqueryのを含めるようにしてください、あなたは確認しましたコンソールのエラー? ')'が足りないだけです。 'console.log($(this).text());' – dariru

答えて

0

..あなたは、構文エラー($(this).text()はあなたが)

$("#mytable > thead > tr > th").each(function() { 
 
    console.log($(this).text()); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="mytable"> 
 
    <thead> 
 
     <tr> 
 
     <th>Head1</th> 
 
     <th>Head2</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
     ..body stuff.. 
 
    </tbody> 
 
</table>

重要を忘れてしまった($(this).text())する必要があります持っているコンソールに目を保つ:

関連する問題