css隔行換色

來源:互聯網
上載者:User
我通過一個最簡單的例子來測試,結果發現我還是喜歡用jq來實現,呵呵。雖然很簡單,但是還記下來了,學習的過程,哈哈。。。html檔案:<html>
<head>
  <title>隔行換色</title>
</head>
<body>
 <ul>
    <li>111111</li>
    <li>222222</li>
    <li>333333</li>
    <li>444444</li>
 </ul>
  </body>
</html> 第一種方法,通過jQuery來實現(別忘記匯入jQuery.js哦):  <script>
    $(document).ready(function(){
  $("li:odd").css("background","#9FB7F6");
  $("li:even").css("background","#B6C8F8");
 });
  </script> 第二種方法,通過css實現<style type="text/css">
UL.myul1 LI{background-color: expression(this.sourceIndex%2==0?'#9FB7F6':'#B6C8F8');
}
</style> 第三種方法,通過CSS和JS實現(註:此處JS盡量加在ul後面,不要加在<head>裡面。當然也可以用window.onload。)  <style type="text/css">
    .li01{background:#9FB7F6;}
   .li02{background:#B6C8F8;}
  </style> <script language="JavaScript">
 objName=document.getElementsByTagName("li") for (i=0;i<objName.length;i++) {
 (i%2==0)?(objName(i).className = "li01"):(objName(i).className = "li02"); }
 </script>
相關文章

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.