• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    HTML n种方式实现隔行变色的示例代码

    发布者: 竹韵 | 发布时间: 2025-8-13 06:59| 查看数: 53| 评论数: 0|帖子模式

    本文主要介绍了HTML n种方式实现隔行变色的示例代码,分享给大家,具体如下:
    n种方式实现隔行变色
    1. <!DOCTYPE html>
    2. <html lang="en">

    3. <head>
    4.     <meta charset="UTF-8">
    5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7.     <title>n种方式实现隔行变色</title>
    8.     <style>
    9.         .box {
    10.             margin: 20px auto;
    11.             width: 300px;

    12.         }

    13.         .box li {
    14.             line-height: 35px;
    15.             border-bottom: 1px dashed #AAA;
    16.             padding: 0 5px;
    17.             text-overflow: ellipsis;
    18.             white-space: nowrap;
    19.             overflow: hidden;
    20.             cursor: pointer;
    21.         }

    22.         /* 1.css3第一种方式 */
    23.         /* .box li:nth-of-type(3n+1){
    24.             background-color: green;
    25.         }
    26.         .box li:nth-of-type(3n+2){
    27.             background-color: red;
    28.         }
    29.         .box li:nth-of-type(3n){
    30.             background-color: blue;
    31.         } */
    32.         /* //=> bgColor与ulList组合2种方式实现 */
    33.         /* .bgColorYellow {
    34.             background-color: yellow;
    35.         }
    36.         .bgColorRed {
    37.             background-color: red;
    38.         }
    39.         .bgColorBlue {
    40.             background-color: blue;
    41.         }  */
    42.         /* //=> bgColor与ulList组合1种方式实现 */
    43.         .bg0 {
    44.             background-color: lightcoral;
    45.         }

    46.         .bg1 {
    47.             background-color: lightgreen;
    48.         }

    49.         .bg2 {
    50.             background-color: lightskyblue;
    51.         }
    52.          #hover {
    53.            background-color: red;
    54.         }
    55.         /* 我们把hover放在bgn的后面,当元素的class=‘bgo’以bgo样式为主 */
    56.         .hover {
    57.            background-color: blueviolet;
    58.         }
    59.     </style>
    60. </head>

    61. <body>
    62.     <ul class="box" id="box">
    63.         <li>上次大家成都你cdsvdvd vax v a 杀虫水</li>
    64.         <li>撒差多少VCD深V上次的深V但是是的深V的深V是DVD深V的深V的深V是大Vsad深V是的v</li>
    65.         <li>大SAV吃撒撒发顺丰</li>
    66.         <li>萨芬从深V撒VCDVD深V是大V撒大V大是发大V是大V是哒但是啥的 </li>
    67.         <li>撒房产税才是</li>
    68.         <li>阿深V大SAV的在v</li>
    69.         <li>上次大家成都你cdsvdvd vax v a 杀虫水</li>
    70.         <!-- /*==利用css优先级搞定:默认背景颜色基于样式类完成,鼠标滑过的样式比样式类优先级高一些(ID
    71.         选择器/行内样式) -->
    72.     </ul>
    73.     <script>
    74.         //=>隔三行变色高亮选中::修改元素的class样式类
    75.             // 样式表: ID选择器
    76.             //        标签选择题
    77.             //        样式类选择器
    78.             //        行内样式

    79.             //        这些方式存在优先级的问题:行内,ID,样式类,标签...
    80.             
    81.         // 方案:
    82.         //    1.依次遍历每一个li,通过索引除以3取余数,让当前的li有不同的背景色
    83.         //    2.第一种的技巧,告别一个个的判断,直接采用数组或者直接找到对应的样式的方式来完成
    84.         //    3.不是遍历每一个li,而是遍历每一组
    85.         var oBox = document.getElementById('box');
    86.         var ulList = oBox.getElementsByTagName('li');
    87.         //*高亮第3种方式:
    88.         for (var i=0; i<ulList.length; i++){
    89.                         ulList[i].className = 'bg'+ (i%3);
    90.                         //=>鼠标滑过:在原有样式类基础上累加一个hover样式类(由于hover在样式类中靠后,它的样式会覆盖原有的bg中的样式)
    91.                         //=>鼠标离开:把新增的hover样式类移除掉即可
    92.                         ulList[i].onmouseover = function (){
    93.                             this.className += 'hover'
    94.                         }
    95.                         ulList[i].onmouseout = function (){
    96.                            // this.className = 'bg0 hover'- 'hover';这不是字符串相减,这是数学运算结果是(NaN)
    97.                             this.className = this.className.replace('hover','');
    98.                         }
    99.                     }
    100.         //=>2.js第一种方式
    101.                 // for (var i = 0; i < ulList.length; i++) {
    102.                 //     //=> 分析:i=0 第一个li i%3=0
    103.                 //     //=> i=1 第一个li i%3=1
    104.                 //     //=>  i=2 第一个li i%3=2
    105.                 //     //=>  i=3 第一个li i%3=0
    106.                 //     var n=i%3; //当前循环找出来的那个li
    107.                 //     liColor=ulList[i];
    108.                     // if(n===0){
    109.                     //     liColor.style.backgroundColor='red';
    110.                     // }else if(n===1){
    111.                     //     liColor.style.backgroundColor='yellow';
    112.                     // }else {
    113.                     //     liColor.style.backgroundColor='pink';
    114.                     // }
    115.                     // }

    116.             //=>3.js第二种方式
    117.                     //  for (var i=0; i<ulList.length; i++){
    118.                     //      switch ( i % 3) {
    119.                     //           case 0:
    120.                     //            ulList[i].className = "bgColorYellow";
    121.                     //            break;
    122.                     //            case 1:
    123.                     //            ulList[i].className = "bgColorRed";
    124.                     //            break;
    125.                     //            case 2:
    126.                     //            ulList[i].className = "bgColorBlue";
    127.                     //            break;

    128.                     //      }
    129.                     //  }
    130.             //=>4.js第三种方式  colorArray+bgColorYellow...
    131.                //  var colorArray = ["bgColorYellow","bgColorRed", "bgColorBlue"];
    132.                // for (var i=0; i<ulList.length; i++){
    133.                      //=> 分析: i%3=0 "bgColorYellow" colorArray[0]
    134.                      //=>        i%3=1  "bgColorBlue"  colorArray[1]
    135.                      //=>        i%3=2  "bgColorRed"   colorArray[2]
    136.                      //=> i%3的余数是多少?就是我们当前需要到数组中通过此索引找到的样式类,而这个样式类则是当前li需要设置的class
    137.                    //       ulList[i].className = colorArray[i % 3];
    138.                           
    139.                  //   }
    140.             //=>5.js第四种方式
    141.                     // for (var i=0; i<ulList.length; i++){
    142.                     //     ulList[i].className = 'bg'+ (i%3); //=>隔三行变色修改样式类
    143.                     //     //=>在改变之前把原有的样式类信息存储到自定义属性中
    144.                     //     ulList[i].myOldClass= ulList[i].className;

    145.                     //     ulList[i].onmouseover = function () {
    146.                     //         // 高亮选中:
    147.                     //         //this:当前操作的元素
    148.                     //         //=>第一种方法
    149.                     //                 //  this.style.background = 'yellow';
    150.                     //          //=>第二种方法
    151.                     //                 // this.id = 'hover';
    152.                     //         //=>第三种方法
    153.                     //           //=>设置新样式之前把原有的样式保存起来,this:当前操作的元素也是一个元素对象,有很多内置属性,我们设置一个自定义属性,把原有的样式类信息存储进来

    154.                     //           console.dir(this);
    155.                               
    156.                     //                //=>滑过,简单粗暴的让class等于hover
    157.                     //                  this.className = 'hover';
    158.                     //             }
    159.                     //             ulList[i].onmouseout = function() {
    160.                     //                // this.style.background = '';
    161.                     //                //    this.id = '';
    162.                     //                //=>离开:让其还原为原有的样式(原有的样式可能是bg0,bg1,bg2)
    163.                     //                this.className=this.myOldClass;

    164.                     //             }
    165.                     // }
    166.            //=>6.js第五种方式三元运算符三种写法  
    167.                      //=>第一种写法
    168.                         // function changeColor() {
    169.                         //     for (var i = 0 ; i< ulList.length; i++){
    170.                         //         ulList[i].style.backgroundColor = i % 3 == 0 ? 'lightblue': ((i%3 ==1)?'lightgreen':'lightpink');
    171.                         //     }
    172.                         // }
    173.                         // changeColor();
    174.                      //=>第二种写法
    175.                         // for (var i = 0; i < ulList.length; i++) {
    176.                         //     var n = i % 3;
    177.                         //     liColor = ulList[i];
    178.                         //        //=>以下两种都可以
    179.                         //             // n === 0 ? liColor.style.backgroundColor = 'red' : (n === 1 ? liColor.style.backgroundColor = 'yellow' :
    180.                         //           //     liColor.style.backgroundColor = 'pink')
    181.                         //=>第三种写法
    182.                         //     n === 0 ? liColor.style.backgroundColor='red': n === 1 ?liColor.style.backgroundColor='yellow' : liColor.style.backgroundColor='blue';
    183.                         // }
    184.              //=>7.js第六种方式
    185.                  //=>我们每一组循环一次,在循环体中,我们把当前这一组样式都设置好即可(可能出现当前这一组不够3个,就报错了)
    186.                     // var max = ulList.length - 1;
    187.                     // for (var i=0;i<ulList.length;i+=3){
    188.                     //     ulList[i].style.background='bg0';
    189.                     //     i + 1 <= max ? ulList[i+1].style.background='bg1':null;
    190.                     //     i + 2 <= max ? ulList[i+2].style.background='bg2':null;
    191.                     // }
    192.     </script>
    193. </body>

    194. </html>
    复制代码
    运行效果如下:

    到此这篇关于HTML n种方式实现隔行变色的示例代码的文章就介绍到这了,更多相关HTML隔行变色内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!


    来源:互联网
    免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表