JavaScript實現(xiàn)生成動態(tài)表格和動態(tài)效果的方法詳解
今天上午完成了Vue實現(xiàn)一個表格的動態(tài)樣式,那么JavaScript代碼能不能實現(xiàn)同樣的效果呢?這樣也可以學(xué)習(xí)一下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">學(xué)生成績表</h1>
<table cellpadding="1" cellspacing="1" align="center" class="table" bgcolor="#cccccc" id="studentTable">
<tr align="center" class="rowTitleColorSet">
<td>學(xué)號</td>
<td>姓名</td>
<td>語文</td>
<td>數(shù)學(xué)</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)建學(xué)號td
var tdId=document.createElement("td");
//加入學(xué)號
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ù)學(xué)td
var tdMathScore=document.createElement("td");
//加入數(shù)學(xué)
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");
//設(shè)置行樣式
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);
//給行添加事件響應(yīng)
tr.onmouseenter=funcMouseenter;//鼠標(biāo)移入事件
tr.onmouseout=funcMouseout;//鼠標(biāo)移出事件
//第三步:生成表格
//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>效果圖一(初始和鼠標(biāo)移出狀態(tài)):

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

到此這篇關(guān)于JavaScript實現(xiàn)生成動態(tài)表格和動態(tài)效果的方法詳解的文章就介紹到這了,更多相關(guān)JavaScript動態(tài)表格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Bootstrap CSS組件之導(dǎo)航條(navbar)
這篇文章主要為大家詳細介紹了Bootstrap CSS組件之導(dǎo)航條(navbar),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
JavaScript中this關(guān)鍵字用法實例分析
這篇文章主要介紹了JavaScript中this關(guān)鍵字用法,結(jié)合實例形式總結(jié)分析了javascript中this關(guān)鍵字在不同條件下的指向問題與相關(guān)操作技巧,需要的朋友可以參考下2018-08-08
Dropzone.js實現(xiàn)文件拖拽上傳功能(附源碼下載)
dropzone.js是重量輕的JavaScript庫,將HTML元素設(shè)置為一個降落區(qū),并通過Ajax文件被上傳到服務(wù)器。本文給大家詳細介紹Dropzone.js實現(xiàn)文件拖拽上傳功能,需要的朋友參考下吧2016-11-11
Javascript創(chuàng)建Silverlight Plugin以及自定義nonSilverlight和lowSilverl
我們在使用Visual Studio IDE創(chuàng)建Silverlight工程時,默認(rèn)情況下都會自動生成一個用于調(diào)試和預(yù)覽Silverlight的Web工程,該工程包含了html和aspx頁面,以及Silverlight.js腳本文件。2010-06-06
基于JavaScript代碼實現(xiàn)隨機漂浮圖片廣告
在網(wǎng)上有很多這樣的代碼,不過未必符合W3C標(biāo)準(zhǔn),因為在頭部加上<!DOCTYPE html>類似標(biāo)簽之后,漂浮效果就會失效,下面分享一個符合標(biāo)準(zhǔn)的漂浮代碼,使需要的朋友免去大量改造代碼的繁瑣2016-01-01

