How to use nth-child for styling with a table with rowspan?

后端 未结 6 1892
盖世英雄少女心
盖世英雄少女心 2020-12-01 17:37

I have a table that has one row that uses rowspan. So,

相关标签:
6条回答
  • 2020-12-01 18:19

    What seems to work for me is to put a td in the row below with display:none

    <table>
       <tr>
          <td rowspan="2">2 rows</td>
          <td>1 row</td>
       </tr>
       <tr>
          <td style="display:none"></td>
          <td>1 row</td>
       </tr>
    </table>
    
    0 讨论(0)
  • 2020-12-01 18:22

    I had a similar issue and I just overrode the row background with backgrounds on the TD's inside of them. Depending on your desired outcome, this may work for you too?

    tr:nth-child(odd) {
        background: #DDE;
    }
    tr:nth-child(odd) td[rowspan] {
        background: #FFF;
    }
    

    Of course you can override other rows like headers, etc with a class or th.

    0 讨论(0)
  • 2020-12-01 18:22

    Try to separate table by tbody, something like:

    tbody tr:nth-child(odd){
      background: #00FFFF;
    }
    tbody tr:nth-child(even){
      background: #FF0000;
    }
    
    tbody:nth-child(odd)  td[rowspan]{
      background: #00FFFF;
    }
    tbody:nth-child(even)  td[rowspan]{
      background: #FF0000;
    }  
    <table>
    <tbody>
    <tr>
    	<td rowspan="4">...</td>
    	<td>...</td>
    	<td>...</td>
    	<td rowspan="4">...</td>
    	<td>...</td>
    </tr>
    <tr>
    	<td>...</td>
    	<td>...</td>
    	<td>...</td>
    </tr>
    <tr>
    	<td>...</td>
    	<td>...</td>
    	<td>...</td>
    </tr>
    <tr>
    	<td>...</td>
    	<td>...</td>
        <td>...</td>
    </tr>
    </tbody>
    <tbody>    
    <tr>
    	<td rowspan="3">...</td>
    	<td>...</td>
    	<td>...</td>
    	<td rowspan="3">...</td>
    	<td>...</td>
    </tr>
    <tr>
    	<td>...</td>
    	<td>...</td>
    	<td>...</td>
    </tr>
    <tr>
    	<td>...</td>
    	<td>...</td>
    	<td>...</td>
    </tr>
    </tbody>
    </table>
        
      

    0 讨论(0)
  • 2020-12-01 18:25

    I used a combination of previous answer to add tr's with display=none programatically:

    HTML

      <table>
      <tr>
        <td>A</td>
        <td>B</td> 
        <td>C</td>
      </tr>
      <tr>
        <td rowspan=2>1</td>
        <td rowspan=2>2</td>
         <td>sub C 1</td>
      </tr>
      <tr>
        <td>sub C 2</td> 
      </tr>
      <tr>
        <td>3</td>
        <td>4</td>
        <td>5</td>
      </tr>
      <tr>
        <td>6</td>
        <td>7</td>
        <td>8</td>
      </tr>
    </table>
    

    CSS

    table tr:nth-child(2n) {
      background-color: #F8ECE0;
    }
    

    JQUERY

    $( "<tr style='display:none'></tr>" ).insertAfter('tbody tr:has(td[rowspan])');
    

    See the JSfiddle.

    0 讨论(0)
  • 2020-12-01 18:34

    Unfortunately, there's no way to do this with :nth-child() alone, or by using CSS selectors alone for that matter. This has to do with the nature of :nth-child() which selects purely based on an element being the nth child of its parent, as well as with CSS's lack of a parent selector (you can't select a tr only if it doesn't contain a td[rowspan], for example).


    jQuery does have the :has() selector that CSS lacks, though, which you can use in conjunction with :even (not :odd as it's 0-indexed versus :nth-child()'s 1-index) for filtering as an alternative to CSS:

    $('tr:not(:has(td[rowspan])):even')
    

    jsFiddle preview

    0 讨论(0)
  • 2020-12-01 18:43

    You can do this using only CSS if you're willing to add some additional markup. Wrap every "group" of rows in a <tbody> tag. Then add a background color to every odd tbody.

    tbody:nth-child(odd) {
      background-color: yellow;
    }
    <table>
      <tbody>
        <tr>
          <td>tr 1</td>
          <td>...</td>
          <td>...</td>
        </tr>
      </tbody>
      <tbody>
        <tr>
          <td rowspan="2">tr 2+3</td>
          <td>...</td>
          <td>...</td>
        </tr>
        <tr>
          <td>...</td>
          <td>...</td>
        </tr>
      </tbody>
      <tbody>
        <tr>
          <td>tr 4</td>
          <td>...</td>
          <td>...</td>
        </tr>
      </tbody>
      <tbody>
        <tr>
          <td>tr 5</td>
          <td>...</td>
          <td>...</td>
        </tr>
      </tbody>
      <tbody>
        <tr>
          <td>tr 6</td>
          <td>...</td>
          <td>...</td>
        </tr>
      </tbody>
      <tbody>
        <tr>
          <td>tr 7</td>
          <td>...</td>
          <td>...</td>
        </tr>
      </tbody>
      <tbody>
        <tr>
          <td>tr 8</td>
          <td>...</td>
          <td>...</td>
        </tr>
      </tbody>
      <tbody>
        <tr>
          <td>tr 9</td>
          <td>...</td>
          <td>...</td>
        </tr>
      </tbody>
      <tbody>
        <tr>
          <td>tr 10</td>
          <td>...</td>
          <td>...</td>
        </tr>
      </tbody>
    </table>

    0 讨论(0)
提交回复
热议问题
.........