【问题标题】:Select TD elements, except the ones in these parent TR选择 TD 元素,这些父 TR 中的元素除外
【发布时间】:2012-08-30 07:23:51
【问题描述】:

我希望它忽略具有 .ignore 类的父类的 <td>。这是我现在所拥有的,但它对每个<td> 进行了样式设置。

<script>
$('td:nth-child(2)').css('background-color', 'rgb(255, 248, 231)');
</script>

<table>
    <tr>
        <td> </td>
        <td>select</td>
        <td> </td>
    </tr>
    <tr class="ignore">
        <td> </td>
        <td>don't select</td>
        <td> </td>
    </tr>
    <tr>
        <td> </td>
        <td>select</td>
        <td> </td>
    </tr>
</table>

【问题讨论】:

    标签: jquery html-table jquery-selectors


    【解决方案1】:
    $('tr:not(.ignore) td').css('background-color', 'rgb(255, 248, 231)');
    

    DEMO

    【讨论】:

      【解决方案2】:
      $('td:nth-child(2)').not('tr.ignore td').css('background-color', 'rgb(255, 248, 231)');​
      

      http://jsfiddle.net/3TrxJ/

      【讨论】:

        【解决方案3】:

        你可以这样做:

        $('td:nth-child(2)').each(function(){
            if ($(this).closest('tr').hasClass('ignore') === false){
                $(this).css('background-color', 'rgb(255, 248, 231)');
            }
        });
        

        Demo Here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-10-16
          • 1970-01-01
          • 2018-06-17
          • 2013-09-28
          • 1970-01-01
          • 1970-01-01
          • 2013-10-10
          • 1970-01-01
          相关资源
          最近更新 更多