CSS之旅(3):強大的偽選擇器
原文出處: 一線碼農
說到偽選擇器,真的讓我體會到了CSS的無比強大,強大到自己貌似都不認識CSS了,有點C# 6.0中一些語法糖帶給我們的震撼。。。首先我們可以在VS里面提前預覽一下。
可以看到,上面的偽類有很多很多,多的讓我眼都快瞎了。。。下面就挑一些實用性比較強的說一說。
一 :nth-child 偽選擇器
我們知道在jquery中有一種選擇器叫做“子類選擇器”,對應的有:nth-child,:first-child,:last-child,:only-child,這回在CSS中同樣
可以辦到,可以說一定程度上緩解了jquery的壓力,下面簡單舉個例子。
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> ul li:nth-child(1) { color: red; } </style> </head> <body> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> </ul> </body>
可以看到,當我灌的是:nth-child(1)的時候,ul的第一個li的color已經變成red了,如果復雜一點的話,可以將1改成n,瀏覽器在解析css的偽類
選擇器的時候,內部應該會調用相應的方法來解析到對應dom的節點,首先要明白n是從0,步長為1的遞增,這個和jquery的nth-child類似,沒
什么好說的,然后我們嘗試下:first-child 和 last-child。
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> ul li:first-child { color: red; font-weight:800; } ul li:last-child { color: blue; font-weight: 800; } </style> </head> <body> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> </ul> </body> </html>
二 :checked,:unchecked,:disabled,:enabled
同樣在jquery中,有一組選擇器叫做“表單對象屬性“,我們可以看看jquery的在線文檔。
同樣我們很開心的發現,在css中也存在這些屬性。。。是不是開始有點醉了。。。還是先睹為快。
1. disabled,enabled
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> input[type='text']:enabled { border: 1px solid red; } input[type='text']:disabled { border: 1px solid blue; } </style> </head> <body> <form> <input type="text" disabled="disabled" /> <input type="text"/> </form> </body> </html>
2. checked,unchecked
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> form input[type="radio"]:first-child:checked { margin-left: 205px; } </style> </head> <body> <form> <input class="test" type="radio" value="女" /><span>女</span><br/> <input class="test" type="radio" value="男" /><span>男</span> </form> </body> </html>
3. selected
這個在css中雖然沒有原裝的,但是可以用option:checked來替代,比如下面這樣。
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> option:checked { color: red; } </style> </head> <body> <form> <select> <option>1</option> <option>2</option> <option>3</option> </select> </form> </body> </html>
三 empty偽選擇器
這個選擇器有點意思,在jquery中叫做”內容選擇器“,就是用來尋找空元素的,如果玩轉jquery的empty,這個也沒有什么問題,
下面舉個例子,讓第一個空p的背景變色。
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> p:first-child{ width:500px; height:20px; } p:empty { background:red; } </style> </head> <body> <p></p> <p>他好</p> </body> </html>
四:not(xxx) 偽選擇器
同樣這個也是非常經典的not選擇器,在jquery中叫做”基本選擇器“,想起來了沒有???
總的來說,當你看完上面這些,是不是覺得css3中已經融入了一些”腳本處理行為”,這種感覺就是那個css再也不是你曾今認識的那個css了。