ãã¼ãã«ã®td ã®å é ããï¼ã¤ãåå¾ããCSS3 nth-childã»ã¬ã¯ã¿ãä½ãããå¬ãããã¹ã£ã¦è©±ã§çãä¸ãã£ãã
td ã®å é ããï¼ã¤ã«è¦ç´ ãé©ç¨ããã
<table> <tr> <td>1çªç® <! -- âèæ¯è²æå®ããã --> <td>2çªç® <! -- âèæ¯è²æå®ããã --> <td>3çªç® <! -- âèæ¯è²æå®ããã --> <td>4çªç® <td>5çªç® </tr> </table>
ç´ãæãæµ®ãã¶åç
td:nth-child(1),td:nth-child(2),td:nth-child(3){ background-color: red; }
è¦ç´ ã»ã¬ã¯ã¿ã並ã¹ã¦æãã°è¯ãï¼ï¼ãªããªãjQueryã§addClassããï¼
妥åã»ã»ã»ã»ããããªãï¼
è² ããæ°ãããããï¼
ãªããè² ããæ°ããããnth-childç解ã§ãã¦ãªãããããï¼
調ã¹ã¾ããã
MDNã¨W3Cãèªãã§ã¿ã
ã¿ã¤ããï¼ï¼ï¼ï¼ï¼ï¼ï¼
span:nth-child(-n+3) 親è¦ç´ ã®å é ãã 3 ã¤ç®ã¾ã§ã®åè¦ç´ ã®ãã¡ã® 1 㤠ã§ãspan ã§ãããã®ãããããã¾ãã:nth-child - CSS | MDN
nth-childã£ã¦ã©ããªã£ã¦ãã®ï¼èãã¦ã¿ã
æ®æ®µä½æ°ãªã使ã£ã¦ãnth-childãããã£ã¦ä¸ä½ã©ããããã¨ï¼ã¡ãã£ã¨èãã¦ã¿ã
è¦ç´ ãï¼åã®TDãããã¨ä»®å®ãã¾ã
次ã®ãããªHTMLãããã¨ãnth-childã¯ã©ãããåä½ãããã®ã
<table> <tr> <td>1çªç® <td>2çªç® <td>3çªç® <td>4çªç® <td>5çªç® </tr> </table>
td:nth-child(2n+1) ã®å ´å
td:nth-child(2n+1)
ã®å ´å
n : 2n+1 = x # nth-child(x) n=0: 2*0+1= 1 # nth-child(1) è¦ç´ ãã n=1: 2*1+1= 3 # nth-child(3) è¦ç´ ãã n=2: 2*2+1= 5 # nth-child(5) è¦ç´ ãã n=3: 2*3+1= 7 # nth-child(7) è¦ç´ ãªã n=4: 2*4+1= 9 # nth-child(9) è¦ç´ ãªã
td:nth-child(2n-1) ã®å ´å
td:nth-child(2n-1)
ã®å ´åãè¦ç´ æ°ã¯ï¼
n : 2n-1 = x # nth-child(x) n=0: 2*0-1= -1 # nth-child(-1) è¦ç´ ãªã n=1: 2*1-1= 1 # nth-child( 1) è¦ç´ ãã n=2: 2*2-1= 3 # nth-child( 3) è¦ç´ ãã n=3: 2*3-1= 5 # nth-child( 5) è¦ç´ ãã n=4: 2*4-1= 7 # nth-child( 7) è¦ç´ ãªã
td:nth-child(-n+3) ã®å ´å
td:nth-child(-n+3)
ã®å ´åãè¦ç´ æ°ã¯ï¼
n : -n+4 = x # nth-child(x) n=0: -1 * 0+4 = 3 # nth-child(3) è¦ç´ ãã n=1: -1 * 1+4 = 2 # nth-child(2) è¦ç´ ãã n=2: -1 * 2+4 = 1 # nth-child(1) è¦ç´ ãã n=3: -1 * 3+4 = 0 # nth-child(0) è¦ç´ ãªã n=4: -1 * 4+4 = -1 # nth-child(-1) è¦ç´ ãªã
td:nth-child(-n+4) ã®å ´å
td:nth-child(-n+4)
ã®å ´åãè¦ç´ æ°ã¯ï¼
n : -n+4 = x # nth-child(x) n=0: -1 * 0+4 = 4 # nth-child(4) è¦ç´ ãã n=1: -1 * 1+4 = 3 # nth-child(3) è¦ç´ ãã n=2: -1 * 2+4 = 2 # nth-child(2) è¦ç´ ãã n=3: -1 * 3+4 = 1 # nth-child(1) è¦ç´ ãã n=4: -1 * 4+4 = 0 # nth-child(0) è¦ç´ ãªã
çµè«ï¼ -n + 3 ã§å é ããï¼ã¤ãåå¾
nth-childã®æ°å¤è©ä¾¡ãèããã°ãå é ããï½åã®åå¾ã¯ãæå¤ã¨ã·ã³ãã«ã«ç解ã§ãã
td:nth-child(-n+3)
å é ããï¼ã¤ã®è¦ç´ ãé¸ã¶CSSãããã¾ã
nth-child( an + b ) ã¯ãã£ã¨æ´»ç¨ãã¦ãããã¨æãã¾ããã
追è¨
å é ã®ï¼ã¤ä»¥å¤ãé¸æããCSSã»ã¬ã¯ã¿
ã»ã¬ã¯ãã§ããããã®éãå¤åå¿ è¦ã«ãªãã¾ãã
$("td:nth-child(n+2)")
ã¾ã㯠not ãã¤ãã
$("td:not(:nth-child(-n+2))")