通常連結是 <a href ...>, 但是都沒法一眼看出, 到底是一般連結還是某種類型的檔案(除非滑鼠移到連結上, 看檔名), 拜讀兩篇文章, 用 CSS 去判斷 副檔名 或 class name 來給他小圖.
目前我也在副檔名是 pdf 的加上小圖, 其它的以後有空再來改~ :p
以下參考自兩位 CSS 大師的文章:
使用 CSS 於 連結後面 加入 icon 的方法如下:(此為我的範例)
a[href $='.pdf'] {
padding-right: 18px;background: transparent url(/images/icon_pdf.gif) no-repeat center right;
}
a[href $='.doc'] {
padding-right: 18px;background: transparent url(/images/icon_doc.gif) no-repeat center right;
}
a[href $='.xls'] {
padding-right: 18px;background: transparent url(/images/icon_xls.gif) no-repeat center right;
}
a[href ^="mailto:"] {
padding-right: 20px;background: transparent url(/images/icon_mail.gif) no-repeat center right;
}
a[class ="popup"] {
padding-right: 18px;background: transparent url(/images/icon_popup.gif) no-repeat center right;
}
a[class ~="popup"] {
padding-right: 18px;background: transparent url(/images/icon_popup.gif) no-repeat center right;
}
a[class ="external"] {
padding-right: 18px;background: transparent url(/images/icon_external.gif) no-repeat center right;
}
a 後面能夠有以下幾種判斷法:
- [foo] -- Has an attribute named "foo"
- [foo="bar"] -- Has an attribute named "foo" with a value of "bar" ("bar")
- [foo~="bar"] -- Value has the word "bar" in it somewhere ("blue bar stools")
- [foo^="bar"] -- Value begins with "bar" ("barstool")
- [foo$="bar"] -- Value ends with "bar" ("I was at the bar")
- [foo*="bar"] -- Value has bar somewhere ("I was looking for barstools")
記得加的時後, icon 要準備好 🙂
感謝 Haoming 提供他改寫的版本(可連到他的網站下載), 超方便.
我又基於他的版本再改寫一下, 主要是針對外部連結我再秀小圖(我外部連結通常都會有 target="blank", 所以改寫如下:
#content a[href $='.pdf'] {
padding-right: 18px;background: transparent url(http://farm1.static.flickr.com/137/362353384_27b2da60cf_m.jpg) no-repeat center right;
}
#content a[href $='.doc'] {
padding-right: 18px;background: transparent url(http://farm1.static.flickr.com/129/362353383_86d1c5ae90_m.jpg) no-repeat center right;
}
#content a[href $='.xls'] {
padding-right: 18px;background: transparent url(http://farm1.static.flickr.com/150/362353380_d48d22e8f8_m.jpg) no-repeat center right;
}
#content a[href ^="mailto:"] {
padding-right: 20px;background: transparent url(http://farm1.static.flickr.com/157/362353387_177cbdea21_m.jpg) no-repeat center right;
}
#content a[target ="_blank"] {
padding-right: 18px;background: transparent url(http://farm1.static.flickr.com/128/362353389_fe347a775c_m.jpg) no-repeat center right;
}