2017-02-09 6 views
0

이 같은 약 테이블이 있습니다신뢰할 수있는 열 수를 여러 <tr>의

<table> 
    <thead> 
    <tr> 
    <th>Mars</th> 
    <th>Venus</th> 
    <th>Jupiter</th> 
    <th>Pluto</th> 
    </tr> 
    <tr> 
    <th scope="col">Produced</th> 
    <th scope="col">Sold</th> 
    <th scope="col">Produced</th> 
    <th scope="col">Sold</th> 
    </tr> 
    </thead> 
    <tr> 
    <td>1</td> 
    <td>50,000</td> 
    <td>30,000</td> 
    <td>100,000</td> 
    <td>80,000</td> 
    </tr> 
    <tr> 
    <td>2</td> 
    <td>10,000</td> 
    <td>5,000</td> 
    <td>12,000</td> 
    <td>9,000</td> 
    </tr> 
</table> 

데이터를 무시 - 정말 나쁜합니다.

tr 아래의 th의 수를 계산하려고하는데 thead 내에 있습니다.

jQuery를 사용하여 다음과 같이했습니다. $($0).find('thead th').length 열 개수를 얻으려고했지만, 기술적으로 8 아래에 있기 때문에 제 신청서에 8이 생깁니다. 나는 단지 tr에있는 th을 모두 계산하는 솔루션을 원합니다. (내 테이블에 trthead 내에 여러 개 있다고 가정합니다.)

기본적으로 신뢰할 수있는 방법으로 테이블의 열 수를 계산하려고합니다.

편집 :

여기

있는 CodePen : http://codepen.io/anon/pen/OWByqW

답변

1

꽤 간단합니다.

var ths = document.querySelector('thead tr:last-child'); 

console.log(ths.childElementCount); 
+0

이것은 가장 간단하고 읽기 쉬운 솔루션입니다. 감사! – theGreenCabbage