2016-12-10 3 views
1

を選択する:Tryint私はこのているTDテキスト

$(document).ready(Principal); 
 
function Principal(){ 
 
\t var howMuch = $('[class^="toShow"]').length; 
 
\t for(var i=0; i<howMuch; i++){ 
 
\t \t console.log($('[class^="toShow"]')[i]); 
 
\t } 
 
}
<table> 
 
\t <tr><td class="toShow">text1</td></tr> 
 
\t <tr><td class="toShow">text2</td></tr> 
 
</table> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

そして、あなたが見ることができるように、私はコンソールによってその全体TDを表示していますが、私はちょうどその2内のテキストを必要としますtds、この場合、 "text1"と "text2"。

私は...)[i].text, .content, .val(), .value(), .contentにしようと試みてきました..しかし、それは未定義の表示..

は、そのテキストを取得する方法はありますか?

+0

を使用し、とても簡単、ありがとう! –

+2

正確なクラスの属性セレクタをやっているのはなぜですか? – Taplar

答えて

3

だけすごい$.fn.text()またはNode.textContent

$(document).ready(Principal); 
 
function Principal(){ 
 
    $('[class^="toShow"]').each(function() { 
 
     console.log($(this).text()); 
 
     console.log(this.textContent); 
 
    }); 
 
}
<table> 
 
\t <tr><td class="toShow">text1</td></tr> 
 
\t <tr><td class="toShow">text2</td></tr> 
 
</table> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

関連する問題