【问题标题】:How to select first and last <th> only?如何仅选择第一个和最后一个 <th>?
【发布时间】:2011-05-28 18:26:31
【问题描述】:

我的表有id="mytable"。我正在尝试仅在第一个和最后一个 &lt;th&gt; 上应用一些 CSS。我试过了,但它不起作用。

#mytable th:first,
#mytable th:last{
 //css goes here
}

【问题讨论】:

    标签: css html-table


    【解决方案1】:
    #mytable th:last-child, #mytable th:first-child {
     //css goes here
    }
    

    【讨论】:

    • :first-child 分别。 +1!
    • 记住 :first-child:last-child 属性选择器仍然不支持 IE6
    • @Alan 是否有关于仍在使用 IE6 的浏览器百分比的统计数据?
    • 如果您确实需要断言是否应该使用某些东西,请查看caniuse.com。您是否应该支持古老的浏览器,完全取决于(恕我直言)您的目标受众。
    【解决方案2】:

    对于 CSS

    #myTable thead tr th:first,
    #myTable thead tr th:last{
      /*CSS goes here*/
    }
    

    使用 Jquery(仅适用于最后一个孩子):

    $("#mytable th:last-child").css({
    
    /*CSS goes here*/
    
    });
    

    【讨论】:

    • 没有任何问题提到 jQuery
    猜你喜欢
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 2016-07-24
    • 2013-12-21
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多