
作者:CPP手艺人 来源: CPP手艺人 日期:2026-12-01 18:27:01
这篇文章主要介绍了HTML n种方式实现隔行变色的实现示例代码,文中(zhong)通过示例代码介绍的隔行非常详细,对大(da)家的变色学习或者工作具有一定的参考学习(xi)价值,需要的例代朋友们下(xia)面随着小编来一起学习学习吧
本文主要介绍了HTML n种(zhong)方式实现隔行变色的示例代码,分享给大家,实现具体如下(xia):

n种方式实现隔行(xing)变(bian)色

<!DOCTYPE html>
<html lang='en'>

<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width,隔行 initial-scale=1.0'>
<meta http-equiv='X-UA-Compatible' content="ie=edge">
<title>n种方式实现隔行变色</title>
<style>
.box {
margin: 20px auto;
width: 300px;
}
.box li {
line-height: 35px;
border-bottom: 1px dashed #AAA;
padding: 0 5px;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
cursor: pointer;
}
/* 1.css3第一种方式 */
/* .box li:nth-of-type(3n+1){
background-color: green;
}
.box li:nth-of-type(3n+2){
background-color: red;
}
.box li:nth-of-type(3n){
background-color: blue;
} */
/* //=> bgColor与ulList组合2种方式实现(xian) */
/* .bgColorYellow {
background-color: yellow;
}
.bgColorRed {
background-color: red;
}
.bgColorBlue {
background-color: blue;
} */
/* //=> bgColor与ulList组合1种方式实现(xian) */
.bg0 {
background-color: lightcoral;
}
.bg1 {
background-color: lightgreen;
}
.bg2 {
background-color: lightskyblue;
}
#hover {
background-color: red;
}
/* 我们把hover放在bgn的后面,当元素的变色class=‘bgo’以bgo样式为主 */
.hover {
background-color: blueviolet;
}
</style>
</head>
<body>
<ul class="box" id='box'>
<li>上次大家(jia)成都你cdsvdvd vax v a 杀虫水</li>
<li>撒差多少VCD深V上次的深V但是(shi)是的深V的深V是DVD深V的深V的深V是大Vsad深V是的v</li>
<li>大SAV吃撒撒发顺丰</li>
<li>萨芬从深V撒VCDVD深V是大V撒大V大是发大V是大V是哒但(dan)是啥的 </li>
<li>撒房(fang)产税才是</li>
<li>阿深V大SAV的在v</li>
<li>上次大家成都你(ni)cdsvdvd vax v a 杀虫水</li>
<!-- /*==利用css优先级搞定:默认背景颜色基于样式类完成(cheng),鼠标滑过的例代样式比样式类优先级高一些(ID
选择器/行内样式) -->
</ul>
<script>
//=>隔三行变色高亮选中::修改元素的class样式类
// 样式表: ID选择器
// 标签选择题
// 样(yang)式类选择器
// 行内样式
// 这些方式存在优先(xian)级的问题:行内,ID,实现样式类(lei),隔行标签...
// 方案:
// 1.依次遍历每一个li,变色通过索引除以3取余数,例代让当(dang)前的实(shi)现li有不同的背景色
// 2.第一种的技巧,告别一个个的隔行判断,直接采用数(shu)组或者直(zhi)接找到对应的变色样式的方式(shi)来完成
// 3.不是遍历每一个li,而是(shi)遍历每一组
var oBox = document.getElementById('box');
var ulList = oBox.getElementsByTagName('li');
//*高亮第3种方式:
for (var i=0; i<ulList.length; i++){
ulList[i].className = 'bg'+ (i%3);
//=>鼠标滑过:在原有样式类基础上累加一个hover样式类(由于hover在样式类中靠后(hou),它的样(yang)式(shi)会覆盖原(yuan)有的bg中的样式(shi))
//=>鼠标离开:把新增的(de)hover样式类移除掉即可
ulList[i].onmouseover = function (){
this.className += 'hover'
}
ulList[i].onmouseout = function (){
// this.className = 'bg0 hover'- 'hover';这不是字符串相减,这是(shi)数学运算结果是(NaN)
this.className = this.className.replace('hover','');
}
}
//=>2.js第一种(zhong)方式
// for (var i = 0; i < ulList.length; i++) {
// //=> 分析:i=0 第一个li i%3=0
// //=> i=1 第一个li i%3=1
// //=> i=2 第一个li i%3=2
// //=> i=3 第一个li i%3=0
// var n=i%3; //当前循环找出来的那个li
// liColor=ulList[i];
// if(n===0){
// liColor.style.backgroundColor='red';
// }else if(n===1){
// liColor.style.backgroundColor='yellow';
// }else {
// liColor.style.backgroundColor='pink';
// }
// }
//=>3.js第二种方式
// for (var i=0; i<ulList.length; i++){
// switch ( i % 3) {
// case 0:
// ulList[i].className = "bgColorYellow";
// break;
// case 1:
// ulList[i].className='bgColorRed';
// break;
// case 2:
// ulList[i].className='bgColorBlue';
// break;
// }
// }
//=>4.js第三种方(fang)式 colorArray+bgColorYellow...
// var colorArray = ["bgColorYellow","bgColorRed", "bgColorBlue"];
// for (var i=0; i<ulList.length; i++){
//=> 分析: i%3=0 "bgColorYellow" colorArray[0]
//=> i%3=1 "bgColorBlue" colorArray[1]
//=> i%3=2 "bgColorRed" colorArray[2]
//=> i%3的余数是多少?就是我们当前(qian)需(xu)要(yao)到数组(zu)中通过此(ci)索引找到的样式(shi)类,而这个样式类(lei)则是当前(qian)li需要设置的class
// ulList[i].className = colorArray[i % 3];
// }
//=>5.js第四种方式(shi)
// for (var i=0; i<ulList.length; i++){
// ulList[i].className = 'bg'+ (i%3); //=>隔三行(xing)变色修改(gai)样式类
// //=>在改变之(zhi)前把原有的样(yang)式类信息存储到自定义属性(xing)中
// ulList[i].myOldClass= ulList[i].className;
// ulList[i].onmouseover = function () {
// // 高亮选中:
// //this:当前操作的元素
// //=>第(di)一种方法
// // this.style.background = 'yellow';
// //=>第二种方法
// // this.id = 'hover';
// //=>第三种方法
// //=>设置新样式之前把原有的样(yang)式保存起来,this:当前操作的元素也是一个元素对象,有很多内置属性,我们设置一个自定义属性,把原(yuan)有的样式类信息存储进来
// console.dir(this);
// //=>滑过,简单粗暴的让class等于hover
// this.className = 'hover';
// }
// ulList[i].onmouseout = function() {
// // this.style.background = '';
// // this.id = '';
// //=>离开:让其还原为原有的样式(原有的样式可能是bg0,bg1,bg2)
// this.className=this.myOldClass;
// }
// }
//=>6.js第五种方式三元运算符三种写法
//=>第一种(zhong)写法
// function changeColor() {
// for (var i = 0 ; i< ulList.length; i++){
// ulList[i].style.backgroundColor = i % 3 == 0 ? 'lightblue': ((i%3 ==1)?'lightgreen':'lightpink');
// }
// }
// changeColor();
//=>第二种写法
// for (var i = 0; i < ulList.length; i++) {
// var n = i % 3;
// liColor = ulList[i];
// //=>以下(xia)两种都可以
// // n === 0 ? liColor.style.backgroundColor = 'red' : (n === 1 ? liColor.style.backgroundColor = 'yellow' :
// // liColor.style.backgroundColor = 'pink')
//=>第三种写法
// n === 0 ? liColor.style.backgroundColor='red': n === 1 ?liColor.style.backgroundColor='yellow' : liColor.style.backgroundColor='blue';
// }
//=>7.js第六种方式
//=>我(wo)们每(mei)一组循环一次,在循环体(ti)中,我们把当前这一组样(yang)式都设置好即可(可能出现当前这一组不够3个,就报错了)
// var max = ulList.length - 1;
// for (var i=0;i<ulList.length;i+=3){
// ulList[i].style.background='bg0';
// i + 1 <= max ? ulList[i+1].style.background='bg1':null;
// i + 2 <= max ? ulList[i+2].style.background='bg2':null;
// }
</script>
</body>
</html>
运行效果如下:
到此(ci)这篇关于HTML n种方式(shi)实现(xian)隔行变色的示例代(dai)码的文章就介绍到这(zhe)了,更多相关HTML隔行变色内容请搜索脚本之家(jia)以前的文章或继续浏览下面的相关(guan)文章,希望大家以后多多支持脚本之家!
来源:脚本之家
链接:https://www.jb51.net/web/731071.html