css2.1中的屬性選取器(css高手請繞道)

來源:互聯網
上載者:User

早上看了司徒先生的js版屬性選取器(http://www.cnblogs.com/rubylouvre/archive/2009/10/27/1590102.html),也激發了我深入瞭解css選取器的學習慾望, 整理於此以便日後備查

*:匹配任何元素。

例如: *{margin:0}

E:匹配任何E元素。

例如: div{color:red}

E F:匹配E的所有後代F元素。

E > F:匹配E的所有子F元素。這個選取器與上一個選取器的區別是:E F會匹配E標籤裡面嵌套的所有F標籤,而E > F只會匹配E標籤裡面嵌套的第一層F標籤。

說明:(Ie6以上版本支援)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><title>css屬性選取器</title><br /><style type="text/css"><br />*{color:black;}<br />.cls1 > div {color:red}<br />.cls2 div{color:green}<br /></style><br /></head><br /><body><br /><div class="cls1"><br /><div class="sub">sub div1<br /><div>aaa</div><br /></div><br /><div class="sub">sub div2<br /><div>bbb</div><br /></div><br /></div><br /><hr /><br /><div class="cls2"><br /><div class="sub">sub div1<br /><div>aaa</div><br /></div><br /><div class="sub">sub div2<br /><div>bbb</div><br /></div><br /></div><br /></body><br /></html><br />

運行代碼

E:first-child:匹配第一個E元素。

說明:IE6以上版本支援

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><title>E:first-child</title><br /><style type="text/css"><br />.cls1 div:first-child{color:red}<br /></style><br /></head><br /><body><br /><div class="cls1"><br /><div >我是紅色的</div><br /><div >我是黑色的</div><br /></div><br /></body><br /></html><br />

運行代碼

E:link,E:visited:分別匹配還沒瀏覽過的超連結和已經瀏覽過的超連結。

E:active,E:hover,E:focus:匹配各種使用者動作下的E元素。

說明:IE6以上的版本,允許任何元素都可使用:hover等偽類

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><title>css屬性選取器</title><br /><style type="text/css"><br />.cls1:first-child{color:red}<br />.cls1:focus{border:solid 1px #f00} /*-ie8才開始支援-*/<br />.cls1:hover{background:#ff9} /*-ie7才開始支援-*/<br /></style><br /></head><br /><body><br /><input type="text" class="cls1"></input><br /><input type="text" class="cls1"></input><br /><input type="text" class="cls1"></input><br /></body><br /></html><br />

運行代碼

E + F:匹配與E鄰接的下一個F元素。

說明:(可惡的IE不支援-不管是IE的哪個版本都一樣) 該選取器還有一個非標準的寫法 E ~ F 效果跟E + F一樣(但是~的這種寫法,IE7,IE8能識別)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><title>css屬性選取器</title><br /><style type="text/css"><br />.cls1 + .cls2{border:solid 1px #f00}<br /></style><br /></head><br /><body><br /><input type="text" class="cls1"></input><br /><input type="text" class="cls2"></input><br /><input type="text" class="cls1"></input><br /><input type="text" class="cls2"></input><br /></body><br /></html><br />

運行代碼

E[foo]:匹配設定了foo屬性(不管是什麼值)的E元素。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><title>css屬性選取器</title><br /><style type="text/css"><br />.cls1[email]{border:solid 1px #f00} /*-ie7才開始支援-*/<br /></style><br /></head><br /><body><br /><input type="text" class="cls1" email="yjmyzz@126.com"></input><br /><input type="text" class="cls1"></input><br /></body><br /></html><br />

運行代碼

E[foo="warning"]:匹配任何foo屬性為"warning"的E元素。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><title>css屬性選取器</title><br /><style type="text/css"><br />input[class='cls1']{border:solid 1px #f00} /*--ie7才開始支援(該屬性區分大小寫)--*/<br /></style><br /></head><br /><body><br /><input type="text" class="cls1" value='cls1'></input><br /><input type="text" class="Cls1" value='Cls1'></input><br /><input type="text" class="cls2" value='cls2'></input><br /></body><br /></html><br />

運行代碼

E[foo~="warning"]:匹配任何foo屬性以空白作為分隔,其中一個值是"warning"的E元素。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><title>css屬性選取器</title><br /><style type="text/css"><br />input[class~='cls2']{border:solid 1px #f00} /*--ie7才開始支援(該屬性區分大小寫)--*/<br /></style><br /></head><br /><body><br /><input type="text" class="cls1 cls2" value='cls1 cls2'></input><br /><input type="text" class="Cls1" value='Cls1'></input><br /><input type="text" class="cls2" value='cls2'></input><br /></body><br /></html><br />

運行代碼

E[lang!="en"]:匹配任何lang屬性值以"-"作為分隔字元,而且第一個精確等於"en"的E元素(也匹配lang屬性只有屬性值en的元素)。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><title>css屬性選取器</title><br /><style type="text/css"><br />input[class|='cls1']{border:solid 1px #f00} /*--ie7才開始支援(該屬性區分大小寫)--*/<br /></style><br /></head><br /><body><br /><input type="text" class="cls1-cls2-cls3" value='cls1-cls2-cls3'></input><br /><input type="text" class="cls2-cls1" value='cls2-cls1'></input><br /><input type="text" class="cls1" value='cls1'></input><br /></body><br /></html><br />

運行代碼
E[foo*="abc"]:匹配任何有foo屬性值,且屬性值包含"abc"的E元素。
說明:雖然w3c組織未把該選取器列在標準之中,但是5大瀏覽器都支援(除IE6及IE6以下版本),已經是事實標準

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><title>css屬性選取器</title><br /><style type="text/css"><br />input[class*='cls1']{border:solid 1px #f00} /*--ie7才開始支援(該屬性區分大小寫)--*/<br /></style><br /></head><br /><body><br /><input type="text" class="cls1-cls2-cls3" value='cls1-cls2-cls3'></input><br /><input type="text" class="cls2cls1" value='cls2cls1'></input><br /><input type="text" class="Cls1" value='Cls1'></input><br /></body><br /></html><br />

運行代碼 

E.warning:相當於E[class~="warning"],匹配任何使用了warning樣式類的E元素。

E#myid:相當於E[id='myid'],匹配任何id為myid的E元素。

E:before和E:after ,這是二個很少用的偽類,用於在E元素前後顯示一些內容(IE8才開始支援)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><title>after,before偽類</title><br /><style type="text/css"><br />*{font-size:12px;line-height:20px;}<br />.cls1:before{content:"歡迎光臨我的網站";display:block;border-bottom:solid 1px #ccc;line-height:25px;}<br />.cls1:after{content:"著作權 2009";display:block;border-top:solid 1px #ccc;line-height:25px;}<br /></style><br /></head><br /><body><br /><p class="cls1">這是一段文字,示範偽類的用法,只有在IE8或Safari,Chrome,FireFox,Chrome,Opera上才能看見before,after的內容</p><br /></body><br /></html><br />

運行代碼 

以上屬性選取器可以在http://www.w3.org/TR/CSS2/selector.html查到官方權威資訊

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.