JavaScript實現(xiàn)生成動態(tài)表格和動態(tài)效果的方法詳解
今天上午完成了Vue實現(xiàn)一個表格的動態(tài)樣式,那么JavaScript代碼能不能實現(xiàn)同樣的效果呢?這樣也可以學習一下JavaScript的語法,晚上試了一下,完全可以,效果一模一樣。
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript->動態(tài)生成漂亮的表格</title>
<style>
.table tr{
line-height:200%;
}
.table td{
width: 100px;
}
.rowTitleColorSet{background-color: #818080;color: rgb(232, 232, 232);text-align: center;}
.evenRowColorSet{background-color: #efefef;color: rgb(8, 8, 8);text-align: center}
.oddRowColorSet{background-color: #f8f8f8;color: rgb(128, 128, 128);text-align: center}
.focusRowColorSet{background-color: #999;color:#d70008;text-align: center}
</style>
</head>
<body onload="init()">
<div id="demo">
<h1 align="center">學生成績表</h1>
<table cellpadding="1" cellspacing="1" align="center" class="table" bgcolor="#cccccc" id="studentTable">
<tr align="center" class="rowTitleColorSet">
<td>學號</td>
<td>姓名</td>
<td>語文</td>
<td>數(shù)學</td>
<td>總分</td>
</tr>
</table>
</div>
</body>
<script>
function init(){
//創(chuàng)建studentList對象
var studentList=[
{Id:101,Name:'小明',ChineseScore:81.5,MathScore:87},
{Id:102,Name:'小黃',ChineseScore:61,MathScore:47.5},
{Id:103,Name:'小麗',ChineseScore:89.5,MathScore:83},
{Id:104,Name:'小宋',ChineseScore:56,MathScore:97},
{Id:105,Name:'小王',ChineseScore:82,MathScore:73},
{Id:106,Name:'小李',ChineseScore:31,MathScore:63},
{Id:107,Name:'小華',ChineseScore:49,MathScore:83},
]
//生成表格
for(item in studentList){
//第一步:創(chuàng)建td
//創(chuàng)建學號td
var tdId=document.createElement("td");
//加入學號
tdId.appendChild(document.createTextNode(studentList[item].Id));
//創(chuàng)建姓名td
var tdName=document.createElement("td");
//加入姓名
tdName.appendChild(document.createTextNode(studentList[item].Name));
//創(chuàng)建語文td
var tdChineseScore=document.createElement("td");
//加入語文
tdChineseScore.appendChild(document.createTextNode(studentList[item].ChineseScore));
//創(chuàng)建數(shù)學td
var tdMathScore=document.createElement("td");
//加入數(shù)學
tdMathScore.appendChild(document.createTextNode(studentList[item].MathScore));
//創(chuàng)建總分td
var tdTotalScore=document.createElement("td");
//加入總分
tdTotalScore.appendChild(document.createTextNode(studentList[item].MathScore+studentList[item].MathScore));
//第二步:生成tr
var tr=document.createElement("tr");
//設置行樣式
if(parseInt(item)%2==0){
tr.className="evenRowColorSet"
}else{
tr.className="oddRowColorSet"
}
tr.appendChild(tdId);
tr.appendChild(tdName);
tr.appendChild(tdChineseScore);
tr.appendChild(tdMathScore);
tr.appendChild(tdTotalScore);
//給行添加事件響應
tr.onmouseenter=funcMouseenter;//鼠標移入事件
tr.onmouseout=funcMouseout;//鼠標移出事件
//第三步:生成表格
//var table=document.getElementsByTagName("table")[0];//雖然也可以但不建議使用
var table=document.getElementById("studentTable");//用這個好
table.appendChild(tr);
}
}
function funcMouseenter(event){
this.className='focusRowColorSet'
}
function funcMouseout(event){
var studentID=this.cells[0].innerHTML;
if(parseInt(studentID)%2==0){
this.className="evenRowColorSet"
}else{
this.className="oddRowColorSet"
}
}
</script>
</html>效果圖一(初始和鼠標移出狀態(tài)):

效果圖二(鼠標移入狀態(tài)):

到此這篇關于JavaScript實現(xiàn)生成動態(tài)表格和動態(tài)效果的方法詳解的文章就介紹到這了,更多相關JavaScript動態(tài)表格內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JS實現(xiàn)將數(shù)據(jù)導出到Excel的方法詳解
這篇文章主要為大家介紹了JavaScript實現(xiàn)將數(shù)據(jù)導出到Excel的兩種方法詳解,文中的示例代碼簡潔易懂,感興趣的小伙伴可以動手嘗試一下2022-06-06
javascript實現(xiàn)iframe框架延時加載的方法
這篇文章主要介紹了javascript實現(xiàn)iframe框架延時加載的方法,可基于setTimeout實現(xiàn)這一功能,是非常實用的技巧,需要的朋友可以參考下2014-10-10
bootstrap-table組合表頭的實現(xiàn)方法
本篇文章主要介紹了bootstrap-table組合表頭的實現(xiàn)方法,非常具有實用價值,需要的朋友可以參考下2017-09-09

