1  

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不能加在head內)

 

<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>
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()