简体   繁体   English

CSS选择器和选择特定元素

[英]Css selectors and selecting specific element

I have a table with this form 我有一张这张桌子

<table class="myclass">
    <thead>
        <tr>
            <th colspan="7">something</th>
        <tr>
        <tr>
            <th>1</th>
            <th>2</th>
            <th>3</th>
            <th>4</th>
            <th>5</th>
            <th>6</th>
            <th>7</th>
        </tr>
    </thead>
    <tbody>
        etc...

I want to apply a css rule only to the th with something. 我只想将css规则应用于某些东西。 That is the descedant of tr which in turn is the first descendant of thead which in turn is descendant of table with myclass I use this css selector 那是tr的后裔,而后者又是thead的第一个后裔,而后者又是myclass表的后裔,我使用此css选择器

table.myclass thead > tr th{
    background-color:red;
}

but is also applied in all the other th's in the thead (1, 2, 3, etc). 但也适用于thead的所有其他th(1、2、3等)。 Have i missunderstood the selector thing? 我是否误解了选择器的内容?

Any good tutorials about advanced selectors? 关于高级选择器的任何好的教程?

And a sample jsfiddle 和一个示例jsfiddle

You need to specify that you only want to select the th that is the descendent of the tr that is the first child of the thead : 您需要指定只选择作为thead第一个孩子tr的后代th

table.myclass thead > tr:first-child th

ref: http://css-tricks.com/almanac/selectors/f/first-child/ 参考: http : //css-tricks.com/almanac/selectors/f/first-child/

fiddle: http://jsfiddle.net/3P9nT/1/ 小提琴: http//jsfiddle.net/3P9nT/1/

As your requirement, you only want to apply CSS for first tr > th . 根据您的要求,您只想将CSS应用于first tr > th So for this you can use :first-child css selecter: 因此,您可以使用:first-child CSS选择器:

table.myclass thead > tr:first-child th{
    background-color:red;
}

Try in fiddle 尝试摆弄

Your fiddle 你的小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM