简体   繁体   English

使用第n个类型的计数器

[英]Using counter with nth type

Is it possible to have a counter with nth-type something like this: 是否可能有一个nth-type的计数器,如下所示:

td:nth-of-type(counter(section)):before { 
     content: "Date"counter(section); 
     counter-increment: section;
}

Where counter is initialized as 计数器初始化为

#myTable  tr { 
    border: 1px solid #ccc; 
    counter-reset: section;counter-increment: section;
}

What I'm trying to do is this - check if tr has class iR and if it does, the structure looks like this: 我想做的是-检查tr是否具有类iR,如果存在,则结构如下所示:

table.myTable td:nth-of-type(1):before { content: "Date"; }
table.myTable td:nth-of-type(2):before { content: "Fajr"; }
table.myTable td:nth-of-type(3):before { content: "Fr"; }
table.myTable td :nth-of-type(4):before { content: "Se"; }
table.myTable td:nth-of-type(5):before { content: "Dr"; }
table.myTable td:nth-of-type(6):before { content: " "; }
table.myTable td:nth-of-type(7):before { content: "A"; }
table.myTable td:nth-of-type(8):before { content: " "; }
table.myTable td:nth-of-type(9):before { content: "Mb"; }
table.myTable td:nth-of-type(10):before { content: " "; }
table.myTable td:nth-of-type(11):before { content: "I"; }
table.myTable td:nth-of-type(12):before { content: ""; }

If not it will be like this 如果没有,它将是这样

table.myTable td:nth-of-type(1):before { content: "Date"; }
table.myTable td:nth-of-type(2):before { content: "Fr"; }
table.myTable td :nth-of-type(3):before { content: "Se"; }
table.myTable td:nth-of-type(4):before { content: "Dr"; }
table.myTable td:nth-of-type(5):before { content: "A"; }
table.myTable td:nth-of-type(6):before { content: "Mb"; }
table.myTable td:nth-of-type(7):before { content: "I"; }

Here is jsfiddle what Im trying to do https://jsfiddle.net/wj5gnafm/1/ 这是jsfiddle我正在尝试做的事情https://jsfiddle.net/wj5gnafm/1/

Based on the fiddle that is now provided in question, you don't even need a counter . 根据现在提供的小提琴,您甚至不需要counter You should be able to achieve this just by using the class and negation selectors that I had mentioned in my original answer. 您只需使用我在原始答案中提到的类和否定选择器就可以实现此目的。 Below is a sample snippet. 下面是一个示例片段。

 td { border: 1px solid; } table tr.iRow td:nth-of-type(1):before { content: 'Date '; } table tr.iRow td:nth-of-type(2):before { content: 'Fr '; } table tr.iRow td:nth-of-type(3):before { content: ''; } table tr.iRow td:nth-of-type(4):before { content: 'Se '; } table tr.iRow td:nth-of-type(5):before { content: 'Dr '; } table tr.iRow td:nth-of-type(6):before { content: ''; } table tr.iRow td:nth-of-type(7):before { content: 'A '; } table tr.iRow td:nth-of-type(8):before { content: ''; } table tr.iRow td:nth-of-type(9):before { content: 'Mb '; } table tr.iRow td:nth-of-type(10):before { content: ''; } table tr.iRow td:nth-of-type(11):before { content: 'I '; } table tr.iRow td:nth-of-type(12):before { content: ''; } table tr:not(.iRow) td:nth-of-type(1):before { content: 'Date '; } table tr:not(.iRow) td:nth-of-type(2):before { content: 'Fr '; } table tr:not(.iRow) td:nth-of-type(3):before { content: 'Se '; } table tr:not(.iRow) td:nth-of-type(4):before { content: 'Dr '; } table tr:not(.iRow) td:nth-of-type(5):before { content: 'A '; } table tr:not(.iRow) td:nth-of-type(6):before { content: 'Mb '; } table tr:not(.iRow) td:nth-of-type(7):before { content: 'I '; } 
 <table> <tr class='iRow'> <td>A</td> <td>B</td> <td>C</td> <td>D</td> <td>E</td> <td>F</td> <td>G</td> <td>H</td> <td>I</td> <td>J</td> <td>K</td> <td>L</td> </tr> <tr> <td>A</td> <td>B</td> <td>C</td> <td>D</td> <td>E</td> <td>E</td> <td>E</td> </tr> </table> 


Answer for original question: 原始问题的答案:

As I had mentioned in my comment to the question, you cannot pass the counter's value as argument to the nth-of-type (or any other nth-* ) selectors. 正如我在对该问题的评论中提到的那样,您不能将计数器的值作为参数传递给nth-of-type (或任何其他nth-* )选择器。

You can achieve your expected output by doing the counter-increment only when the required class is present (by using class selector) and use :not([classname]) to increment the other counter. 仅当所需的类存在时(通过使用类选择器)并使用:not([classname])来增加另一个计数器,才可以通过进行counter-increment来实现预期的输出。

While displaying the value, make use of either the class or the class-negation in the selector and then display the value as required. 在显示值时,使用选择器中的类或类否定,然后根据需要显示值。 Below is a sample snippet. 下面是一个示例片段。

 table { counter-reset: section, section-other; } table, tr, td { border: 1px solid; } table tr.iRow { counter-increment: section; background: sandybrown; /* just for distinction */ } table .iRow td:nth-of-type(1):before { content: "Foo." counter(section)" "; } table .iRow td:nth-of-type(2):before { content: "Bar." counter(section)" "; } table .iRow td:nth-of-type(3):before { content: "Baz." counter(section)" "; } table tr:not(.iRow) { counter-increment: section-other; background: wheat; /* just for distinction */ } table tr:not(.iRow) td:nth-of-type(1):before { content: "ooF." counter(section-other)" "; } table tr:not(.iRow) td:nth-of-type(2):before { content: "raB." counter(section-other)" "; } table tr:not(.iRow) td:nth-of-type(3):before { content: "zaB." counter(section-other)" "; } 
 <table> <tr class='iRow'> <td>A</td><td>B</td><td>C</td> </tr> <tr class='iRow'> <td>A</td><td>B</td><td>C</td> </tr> <tr> <td>A</td><td>B</td><td>C</td> </tr> <tr class='iRow'> <td>A</td><td>B</td><td>C</td> </tr> <tr> <td>A</td><td>B</td><td>C</td> </tr> <tr class='iRow'> <td>A</td><td>B</td><td>C</td> </tr> <tr> <td>A</td><td>B</td><td>C</td> </tr> <tr> <td>A</td><td>B</td><td>C</td> </tr> </table> 

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

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