1

nth-type次のようなカウンターを持つことは可能ですか?

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

カウンターは次のように初期化されます

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

私がやろうとしているのはこれです-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: ""; }

でないとこうなる

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"; }

これが私がやろうとしているjsfiddleです https://jsfiddle.net/wj5gnafm/1/

4

1 に答える 1

2

問題で提供されているフィドルに基づいて、counter. 元の回答で述べたクラスセレクターと否定セレクターを使用するだけで、これを達成できるはずです。以下はサンプルのスニペットです。

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>


元の質問に対する回答:

nth-of-type質問へのコメントで述べたように、カウンターの値を引数として(または他のnth-*) セレクターに渡すことはできません。

counter-increment必要なクラスが存在する場合にのみ (クラスセレクターを使用して) を実行:not([classname])し、他のカウンターをインクリメントするために使用することで、期待される出力を実現できます。

値を表示するときは、セレクターでクラスまたはクラス否定を使用し、必要に応じて値を表示します。以下はサンプルのスニペットです。

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>

于 2015-12-19T05:16:27.977 に答える