2015-01-02 3 views
0

에서 테이블 셀 ID를 가져옵니다 :나는 두 개의 행과 세 개의 열이있는 테이블이 ROWID

<table> 
    <tr id='a'> 
    <td id='aa'>Cell A</td> 
    <td id='ab'>Cell B</td> 
    <td id='ac'>Cell C</td> 
    </tr> 
    <tr id='b'> 
    <td id='ba'>Cell A</td> 
    <td id='bb'>Cell B</td> 
    <td id='bc'>Cell C</td> 
    </tr> 
</table> 

어떻게 자바 스크립트 또는 jQuery를 사용하여 특정 <tr> 아래의 모든 <td> ID를받을 수 있나요?

미리 감사드립니다.

+0

루프를 완료하면 모든 tdid를 가져올 수 없습니다. –

+3

'id','tr'는'rowIndex' 속성을,'td'는'cellIndex' 속성을 사용합니다. – Teemu

+0

td Class Selector (".class")에 대해 tr 및 id에 class 속성을 사용할 수 있습니다. 주어진 클래스의 모든 요소를 ​​선택합니다. –

답변

3

당신은 예를 들어,이 코드는 행 내부의 모든 TDS를 통해 루프를 얻을 수 있어야 TD 식별자 # A

$(document).ready(function(){ 
    $('table #a td').each(function(){ 
     alert($(this).attr('id')); 
    }); 
}); 

볼에서 DEMO

3

을 수행해야 행

$(document).ready(function(){ 
    $('table #a td').each(function(){ 
     alert($(this).attr('id')); 
    }); 
}); 
내부의 모든 TDS을 반복
2

<td>의 모든 ID는 <tr id='a'>입니다.

당신이 다른 행을 원하는 경우

$('#a td').each(function(td){ 
 
    console.log($(this).attr('id')); // check your console after running this 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <tr id='a'> 
 
    <td id='aa'>Cell A</td> 
 
    <td id='ab'>Cell B</td> 
 
    <td id='ac'>Cell C</td> 
 
    </tr> 
 
    <tr id='b'> 
 
    <td id='ba'>Cell A</td> 
 
    <td id='bb'>Cell B</td> 
 
    <td id='bc'>Cell C</td> 
 
    </tr> 
 
</table>
, 그냥 <tr> ID를 변경합니다.

자세히보기 :

1

당신은 일치하는 아이디의의 배열을 반환 map()을 사용할 수 있습니다 아래와 같이

var idArray = $('#a td').map(function(i,elm) { 
 
    return elm.id; 
 
}).get(); 
 
console.log(idArray);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <tr id='a'> 
 
    <td id='aa'>Cell A</td> 
 
    <td id='ab'>Cell B</td> 
 
    <td id='ac'>Cell C</td> 
 
    </tr> 
 
    <tr id='b'> 
 
    <td id='ba'>Cell A</td> 
 
    <td id='bb'>Cell B</td> 
 
    <td id='bc'>Cell C</td> 
 
    </tr> 
 
</table>