2014-03-05 131 views

回答

1

HTML

<ul> 
     <li>First Item</li> 
     <li>Second Item</li> 
     <li>Third Item</li> 
     <li>Fourth Item</li> 
     <li>Fifth Item</li> 
    </ul> 

CSS:

li { 
    background: slategrey; 
} 
/* select the second-last item */ 
li:nth-last-child(2) { 
    background: lightslategrey; 
} 

enter image description here

See more details

Mention in HTML5Please Selectors

將列表添加到HTML5請爲css選擇部分,其中提到了IE8日落之後nth-last-child的可用性。

selectivizr

<script type="text/javascript" src="[JS library]"></script> 
<!--[if (gte IE 6)&(lte IE 8)]> 
    <script type="text/javascript" src="selectivizr.js"></script> 
    <noscript><link rel="stylesheet" href="[fallback css]" /></noscript> 
<![endif]--> 
  • 的使用只包含在你的網頁的標記此腳本。如果您不是 已經在使用JavaScript庫,則需要從下表中選擇一個來自 。

enter image description here

+0

感謝Updation @ alexK85。 – newTag

+0

但是':nth-​​last-child'在IE8上不支持? –

+0

@KheemaPandey是的,請參閱由alexK85更新的鏈接。 http://html5please.com/#selectors – newTag

0

加法:如果你想使用IE7和IE8的選擇,你可以使用Webshim。這是一個polyfill javascript庫,它使HTML5和CSS3選擇器能夠訪問較舊的瀏覽器(大部分時間是IE7和IE8)。它很容易使用和安裝。

這裏是如何使用它的詳細說明的鏈接:http://css-tricks.com/how-to-use-the-webshims-polyfill

希望這有助於你。

0

:nth-child:nth-last-child在IE8上不起作用。您可以使用名爲selectivizrhttp://selectivizr.com/的JavaScript庫。

selectivizr是一個JavaScript實用程序,它模擬Internet Explorer 6-8中的CSS3僞類和屬性選擇器。

可以使用:nth-child

ul li{ list-style-type:none; line-height:1.5em;} 

li:nth-child(odd) { 
    background-color: #ccc; 
} 
li:nth-child(even) { 
    background-color: #eee; 
} 

HTML是這樣做斑馬strippting ..

<ul> 
    <li>ITem1 </li> 
    <li>ITem2 </li> 
    <li>ITem3 </li> 
    <li>ITem4 </li> 
    <li>ITem5 </li> 
    <li>ITem6 </li> 
    </ul> 

這是一個工作演示。 http://jsbin.com/yaculabe/1/edit

+0

@ user3322631我的解決方案對您有幫助嗎? –