我们在操作div ul li的时候一般都是用document.getElementById和document.getElementsTagName选择器来选择元素,而当我们操作表格应用的时候有更简便的JS操作方式;

  比如:tBodies,    //选择表格身体tbody

      tHead,    //选择表格头部,一个表格中只能含有一个头部

      tFoot,    //选择表格尾部,一个表格中只能含有一个尾部

      rows,    //tr的每一行

      cells。    //每一列

<!DOCTYPE html>
<html>
<head>
<title>隔行变色</title>
</head>
<body>
<table border="1" width="500" >
var Otab = document.getElementById('tab1');
var Ocol ='';
for(var i=0;i<Otab.tBodies[0].rows.length;i++){
Otab.tBodies[0].rows[i].onmouseover=function(){
Ocol= this.style.background;
this.style.background='red';
};
Otab.tBodies[0].rows[i].onmouseout=function(){
this.style.background=Ocol;
};
if(i%2){
Otab.tBodies[0].rows[i].style.background='#CCC';
}else{
Otab.tBodies[0].rows[i].style.background='';
}
}

</script>
</body>
</html>

相关文章:

  • 2022-12-23
  • 2021-12-10
  • 2022-12-23
  • 2022-03-05
  • 2022-12-23
  • 2021-08-26
  • 2021-09-30
猜你喜欢
  • 2021-07-20
  • 2021-10-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-14
  • 2021-11-19
相关资源
相似解决方案