JavaScript實(shí)現(xiàn)生成動(dòng)態(tài)表格和動(dòng)態(tài)效果的方法詳解
今天上午完成了Vue實(shí)現(xiàn)一個(gè)表格的動(dòng)態(tài)樣式,那么JavaScript代碼能不能實(shí)現(xiàn)同樣的效果呢?這樣也可以學(xué)習(xí)一下JavaScript的語(yǔ)法,晚上試了一下,完全可以,效果一模一樣。
<!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->動(dòng)態(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é)生成績(jī)表</h1>
<table cellpadding="1" cellspacing="1" align="center" class="table" bgcolor="#cccccc" id="studentTable">
<tr align="center" class="rowTitleColorSet">
<td>學(xué)號(hào)</td>
<td>姓名</td>
<td>語(yǔ)文</td>
<td>數(shù)學(xué)</td>
<td>總分</td>
</tr>
</table>
</div>
</body>
<script>
function init(){
//創(chuàng)建studentList對(duì)象
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é)號(hào)td
var tdId=document.createElement("td");
//加入學(xué)號(hào)
tdId.appendChild(document.createTextNode(studentList[item].Id));
//創(chuàng)建姓名td
var tdName=document.createElement("td");
//加入姓名
tdName.appendChild(document.createTextNode(studentList[item].Name));
//創(chuàng)建語(yǔ)文td
var tdChineseScore=document.createElement("td");
//加入語(yǔ)文
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");//用這個(gè)好
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實(shí)現(xiàn)生成動(dòng)態(tài)表格和動(dòng)態(tài)效果的方法詳解的文章就介紹到這了,更多相關(guān)JavaScript動(dòng)態(tài)表格內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 原生javaScript做得動(dòng)態(tài)表格(注釋寫(xiě)的很清楚)
- js生成動(dòng)態(tài)表格并為每個(gè)單元格添加單擊事件的方法
- JS實(shí)現(xiàn)動(dòng)態(tài)表格的添加,修改,刪除功能(推薦)
- JavaScript實(shí)現(xiàn)簡(jiǎn)單動(dòng)態(tài)表格
- JavaScript實(shí)現(xiàn)動(dòng)態(tài)表格效果
- JavaScript實(shí)現(xiàn)動(dòng)態(tài)表格的方法詳解
- java前端javascript生成動(dòng)態(tài)表格示例演示
- JavaScript實(shí)現(xiàn)動(dòng)態(tài)表格的示例代碼
相關(guān)文章
Bootstrap CSS組件之導(dǎo)航條(navbar)
這篇文章主要為大家詳細(xì)介紹了Bootstrap CSS組件之導(dǎo)航條(navbar),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
JavaScript中this關(guān)鍵字用法實(shí)例分析
這篇文章主要介紹了JavaScript中this關(guān)鍵字用法,結(jié)合實(shí)例形式總結(jié)分析了javascript中this關(guān)鍵字在不同條件下的指向問(wèn)題與相關(guān)操作技巧,需要的朋友可以參考下2018-08-08
Dropzone.js實(shí)現(xiàn)文件拖拽上傳功能(附源碼下載)
dropzone.js是重量輕的JavaScript庫(kù),將HTML元素設(shè)置為一個(gè)降落區(qū),并通過(guò)Ajax文件被上傳到服務(wù)器。本文給大家詳細(xì)介紹Dropzone.js實(shí)現(xiàn)文件拖拽上傳功能,需要的朋友參考下吧2016-11-11
Javascript創(chuàng)建Silverlight Plugin以及自定義nonSilverlight和lowSilverl
我們?cè)谑褂肰isual Studio IDE創(chuàng)建Silverlight工程時(shí),默認(rèn)情況下都會(huì)自動(dòng)生成一個(gè)用于調(diào)試和預(yù)覽Silverlight的Web工程,該工程包含了html和aspx頁(yè)面,以及Silverlight.js腳本文件。2010-06-06
基于JavaScript代碼實(shí)現(xiàn)隨機(jī)漂浮圖片廣告
在網(wǎng)上有很多這樣的代碼,不過(guò)未必符合W3C標(biāo)準(zhǔn),因?yàn)樵陬^部加上<!DOCTYPE html>類似標(biāo)簽之后,漂浮效果就會(huì)失效,下面分享一個(gè)符合標(biāo)準(zhǔn)的漂浮代碼,使需要的朋友免去大量改造代碼的繁瑣2016-01-01

