1

I have multiple column headers that spans of multiple column and I would like to find the correct header number/count for the columns. Also some of the table data have rowspan and colspan.

I want to be able to click any cell or table data and get its left and top headers.

  1. So if I click the with "D9:LH1-TH4(H4a)" I should get:

    • Left Header is: LH1, Top Header1 is TH4 and Top Header 2 is H4a
  2. And if I click the with "D3:LH2-TH2(H2b-H4a)" I should get:

    • Left Header is LH2, Top Header 1 is TH2 and Top Header 2 is H2b

Example:

<table width="200" border="1" style="background-color:#000; color:#FFF">
  <tr>
    <th scope="col">&nbsp;</th>
    <th colspan="2" scope="col"> TH1 </th>
    <th colspan="4" scope="col"> TH2 </th>
    <th colspan="6" scope="col"> TH4 </th>
  </tr>
    <tr>
    <th scope="col">&nbsp;</th>
    <th scope="col"> H1a </th>
    <th scope="col">H1b</th>
    <th scope="col"> H2a</th>
    <th scope="col">H2b</th>
    <th scope="col">H2c</th>
    <th scope="col">H2d</th>
    <th colspan="6" scope="col"> H4a </th>
  </tr>
  <tr>
    <th rowspan="3" scope="row"><span>LH1</span></th>
    <td colspan="2" rowspan="3">D1:LH1-TH1(H1a-H1b)</td>
    <td colspan="3" rowspan="2">D2:LH1-TH2(H2a-H2c)</td>
    <td rowspan="3">D5:LH1-TH2(H2d)</td>
    <td rowspan="3">D6:LH1-TH4(H2a-H2c)</td>
    <td colspan="2">D7:LH1-TH4(H4a)</td>
    <td>D8:LH1-TH4(H4a)</td>
    <td rowspan="3">D13:LH1-TH4(H4a)</td>
    <td rowspan="3">D14:LH1-TH4(H4a)</td>
  </tr>
  <tr>
    <td colspan="3">D9:LH1-TH4(H4a)</td>
  </tr>
  <tr>
    <td colspan="2">D3:LH1-TH2(H2a-H2b)</td>
    <td>D4:LH1-TH2(H2c)</td>
    <td>D10:LH1-TH4(H4a)</td>
    <td>D11:LH1-TH4(H4a)</td>
    <td>D12:LH11-TH4(H4a)</td>
  </tr>
  <tr>
    <th scope="row"><span>LH2</span></th>
    <td colspan="2" >D1:LH2-TH1(H1a-H1b)</td>
    <td colspan="3">D2:LH2-TH2(H2a-H2c)</td>
    <td colspan="5">D3:LH2-TH2(H2b-H4a)</td>
    <td>D4:LH2-TH4(H4a)</td>
    <td>D5:LH2-TH4(H4a)</td>
  </tr>
</table>​

Please see this link

4

2 回答 2

1

这非常棘手。您可以使用父偏移量找到您的标题。

让它在这个小提琴中工作 http://jsfiddle.net/work4liberty/fk6sy/14/

于 2012-04-29T02:18:38.867 回答
0
$('td')​.on('click', function() {
    var text = $(this).text().split(':')[1],
        LH = text.split('-')[0],
        TH = text.split('-')[1].split('(')[0],
        TH2 = text.split('-')[1].split('(')[1];
    console.log('Left header is '+LH+', Top header 1 is '+TH+' and Top header 2 is '+TH2)
})​;​

小提琴

于 2012-04-29T01:05:10.800 回答