热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> 实用代码>>正文内容

隔行变色,划过变色

发布时间:2010年06月22日点击数: 佚名

测试ie6/7/FF都通过;CSS和JS完全分离;加了部分注释【注:下边代码复制即可看到效果】

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5. <title>无标题文档</title> 
  6. <style type="text/css"> 
  7. .container ul{margin:0;padding:0;} 
  8. .container li{cursor:pointer;height:30px;width:200px;background-color:#FFC;line-height:30px; text-indent:1em;font-size:12px;} 
  9. .container .change{cursor:pointer;height:30px;width:200px;background-color:#C1F9CC;line-height:30px; text-indent:1em;font-size:12px;} 
  10. .container .current{cursor:pointer;height:30px;width:200px;background-color:#F66;line-height:30px; text-indent:1em;font-size:12px;} 
  11. </style> 
  12. <script type="text/javascript"> 
  13.  window.onload = function() { 
  14.         var arrayli = document.getElementById('content').getElementsByTagName('li'); 
  15.         var bool = true; //奇数行为true 
  16.         var oldStyle; //保存原有样式 
  17.         for(var i = 0;i<arrayli.length;i++){ 
  18.             //各行变色 
  19.             if(bool === true){ 
  20.                 arrayli[i].className = "change"
  21.                 bool = false
  22.             } 
  23.             else { 
  24.                 arrayli[i].className = ""
  25.                 bool = true;     
  26.             } 
  27.             //划过变色 
  28.             arrayli[i].onmouseover = function() { 
  29.                 oldStyle = this.className; 
  30.                 this.className = "current" 
  31.             }  
  32.             arrayli[i].onmouseout = function() { 
  33.                 this.className = oldStyle
  34.             }  
  35.         } 
  36.     } 
  37. </script> 
  38. </head> 
  39. <body> 
  40.     <div class="container"> 
  41.         <ul id="content"> 
  42.             <li>隔行变色,划过变色</li> 
  43.             <li>隔行变色,划过变色</li> 
  44.             <li>隔行变色,划过变色</li> 
  45.             <li>隔行变色,划过变色</li> 
  46.             <li>隔行变色,划过变色</li> 
  47.             <li>隔行变色,划过变色</li> 
  48.             <li>隔行变色,划过变色</li> 
  49.             <li>隔行变色,划过变色</li> 
  50.             <li>隔行变色,划过变色</li> 
  51.             <li>隔行变色,划过变色</li> 
  52.             <li>隔行变色,划过变色</li> 
  53.         </ul> 
  54.     </div> 
  55. </body> 
  56. </html> 

效果图如下:

支持多个ul的:【注:下边代码复制即可看到效果】

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  5. <title>无标题文档</title> 
  6. <style type="text/css"> 
  7. .container ul{margin:0;padding:0;} 
  8. .container li{cursor:pointer;height:30px;width:200px;background-color:#FFC;line-height:30px; text-indent:1em;font-size:12px;} 
  9. .container .change{cursor:pointer;height:30px;width:200px;background-color:#C1F9CC;line-height:30px; text-indent:1em;font-size:12px;} 
  10. .container .current{cursor:pointer;height:30px;width:200px;background-color:#F66;line-height:30px; text-indent:1em;font-size:12px;} 
  11. </style> 
  12. <script type="text/javascript"> 
  13.  window.onload = function() { 
  14.     changeColor('content'); 
  15.     changeColor('content2');//写上要控制的UL的ID,想控制几个就控制几个 
  16.     changeColor('content3'); 
  17. function changeColor(id) { 
  18.     var arrayli = document.getElementById(id).getElementsByTagName('li'); 
  19.         var bool = true; //奇数行为true 
  20.         var oldStyle; //保存原有样式 
  21.         for(var i = 0;i<arrayli.length;i++){ 
  22.             //各行变色 
  23.             if(bool === true){ 
  24.                 arrayli[i].className = "change"
  25.                 bool = false
  26.             } 
  27.             else { 
  28.                 arrayli[i].className = ""
  29.                 bool = true;     
  30.             } 
  31.             //划过变色 
  32.             arrayli[i].onmouseover = function() { 
  33.                 oldStyle = this.className; 
  34.                 this.className = "current" 
  35.             }  
  36.             arrayli[i].onmouseout = function() { 
  37.                 this.className = oldStyle
  38.             }  
  39.         } 
  40. </script> 
  41. </head> 
  42. <body> 
  43.     <span>第一个UL</span> 
  44.     <div class="container"> 
  45.         <ul id="content"> 
  46.             <li>隔行变色,划过变色</li> 
  47.             <li>隔行变色,划过变色</li> 
  48.             <li>隔行变色,划过变色</li> 
  49.             <li>隔行变色,划过变色</li> 
  50.             <li>隔行变色,划过变色</li> 
  51.         </ul> 
  52.     </div> 
  53.     <br /> 
  54.     <span>第二个UL</span> 
  55.     <div class="container"> 
  56.         <ul id="content2"> 
  57.             <li>隔行变色,划过变色</li> 
  58.             <li>隔行变色,划过变色</li> 
  59.             <li>隔行变色,划过变色</li> 
  60.             <li>隔行变色,划过变色</li> 
  61.             <li>隔行变色,划过变色</li> 
  62.         </ul> 
  63.     </div> 
  64.     <span>第三个UL</span> 
  65.     <div class="container"> 
  66.         <ul id="content3"> 
  67.             <li>隔行变色,划过变色</li> 
  68.             <li>隔行变色,划过变色</li> 
  69.             <li>隔行变色,划过变色</li> 
  70.             <li>隔行变色,划过变色</li> 
  71.             <li>隔行变色,划过变色</li> 
  72.         </ul> 
  73.     </div> 
  74. </body> 
  75. </html> 

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作