:n 番目のセレクター

このセレクターは、主に以下の要素を正確に一致させることができます: :first-child は要素の最初の子要素を選択します; :last-child は要素の最後の子要素を選択します; :nth -child() は 1 つ以上を選択します要素の特定の子要素を選択します。 :nth-last-child() は、この要素の最後の子要素から開始して、要素の 1 つ以上の特定の子要素を選択します。 ;:nth-of-type() は、指定された要素を選択します。 nth-last-of-type() は、最後の要素から数えて、指定された要素を選択します。 :first-of-type は、上位要素の下にある最初の要素を選択します。 :last-of-type は、同じ型の子要素を選択します。親要素と同じ型の最後の子要素。 :only-child は親要素の唯一の要素を選択します。 :only-of-type は要素と同じ型の唯一の子要素を選択します。親要素; :empty で選択された要素にはコンテンツがありません。

1, :first-child

:fist-child
:last-child
:nth-child()
:nth-last-child()
:nth-of-type()
:nth-last-of-type()
:first-of-type
:last-of-type
:only-child
:only-of-type
:empty

css3-selector-nth1.png2, :last-child

.demo li:first-child {background: green; border: 1px dotted blue;}.demo li.first {background: green; border: 1px dotted blue;}

css3-selector-nth2.png3, :nth-child()

.demo li:last-child {background: green; border: 2px dotted blue;}
.demo li.last {background: green; border: 2px dotted blue;}

css3-selector-nth3.pngこの種の式は負の値を参照できませんつまり、li:nth-child(-3) は間違った使用法です。

:nth-child(2n)、このメソッドは前のメソッドのバリエーションです。n の 2 つの倍数を選択できます。もちろん、「2」は必要な数値に置き換えることができます。 2n) {背景: ライム;} .demo

:nth-child(length);/*参数是具体数字*/
:nth-child(n);/*参数是n,n从0开始计算*/
:nth-child(n*length)/*n的倍数选择,n从0开始算*/
:nth-child(n+length);/*选择大于length后面的元素*/
:nth-child(-n+length)/*选择小于length前面的元素*/
:nth-child(n*length+1);/*表示隔几选一*/
//上面length为整数
.demo li:nth-child(3) {background: lime;}

:nth-child(-n+5) と同等 このセレクターは、前の 5 番目のものを選択します

n=0 --》 -n+5=5 --》 5 番目の li が選択されます

n=1 - -》 -n+5=4 --》 4番目のliが選択されました
n=2 --》 -n+5=3 --》 3番目のliが選択されました
n=3 --- 》 -n+5= 2 --》 2番目のli
n=4が選択されます --》 -n+5=1 --》 1番目のli
n=5が選択されます --》 -n+5= 0--》要素は選択されていません


4, :nth-last-child()

li:nth-child(even) {background: lime;}

css3-selector-nth10.png5, :nth-of-type

:nth-of-type は :nth-child に似ていますが、違いはカウントのみであることです。セレクター

6 で指定された要素。 :nth-last-of-type

言うまでもなく、このセレクターは前の :nth-last-child と同じように使用されることを想像できます。要素のタイプ。

7. :first-of-type と :last-of-type

: first-of-type と :last-of-type の 2 つのセレクターは、:first-child と :last-child に似ています。違いは、要素のタイプが指定されていることです。

8、:only-child および :only-of-type

「:only-child」は、要素がその親要素の唯一の子要素であることを意味します。

9、:空

:empty は、コンテンツのない要素を選択するために使用されます。ここでのコンテンツなしとは、スペースであっても、コンテンツがまったくないことを意味します。表示されます



学び続ける
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.php.cn/" /> <title>php中文网</title> <style type="text/css"> li:first-child{ color:green; } </style> </head> <body> <ul> <li>html专区</li> <li>Div+css专区</li> <li>Jquery专区</li> <li>Javascript专区</li> </ul> </body> </html>
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!