基于JS实现动态表格的代码是什么
Admin 2022-11-14 群英技术资讯 576 次浏览
今天上午完成了Vue实现一个表格的动态样式,那么JavaScript代码能不能实现同样的效果呢?这样也可以学习一下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->动态生成漂亮的表格</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>数学</td> <td>总分</td> </tr> </table> </div> </body> <script> function init(){ //创建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){ //第一步:创建td //创建学号td var tdId=document.createElement("td"); //加入学号 tdId.appendChild(document.createTextNode(studentList[item].Id)); //创建姓名td var tdName=document.createElement("td"); //加入姓名 tdName.appendChild(document.createTextNode(studentList[item].Name)); //创建语文td var tdChineseScore=document.createElement("td"); //加入语文 tdChineseScore.appendChild(document.createTextNode(studentList[item].ChineseScore)); //创建数学td var tdMathScore=document.createElement("td"); //加入数学 tdMathScore.appendChild(document.createTextNode(studentList[item].MathScore)); //创建总分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>
效果图一(初始和鼠标移出状态):
效果图二(鼠标移入状态):
到此,关于“基于JS实现动态表格的代码是什么”的学习就结束了,希望能够解决大家的疑惑,另外大家动手实践也很重要,对大家加深理解和学习很有帮助。如果想要学习更多的相关知识,欢迎关注群英网络,小编每天都会给大家分享实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了Vue+Springboot实现接口签名的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要介绍了linux服务器快速卸载安装node环境(简单上手),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要为大家详细介绍了Vue实现简单购物车小案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
目录场景方式一方式二场景使用ElementUI中的Table 表格时,如果列内容超过列宽,会默认换行,如下这样看起来不美观,还有可能引起其它样式问题。那么如何解决呢?方式一使用Table组件自带的show-overflow-tooltip属性参数说明类型可选值默认值show-overflow-tooltip当内容过长被
今天我们来了解的是vue缓存页面怎样管理的内容,对于vue缓存页面的管理的问题的就是什么时候销毁及如何销毁,那么具体怎样做呢?下面跟随小编一起来了解看看吧。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008