在js中动态打印表格

下面这个html可以在js中动态操作表格

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>生成表格</title>

<style type="text/css">
<!--
.STYLE1 {
	font-size: large;
	font-weight: bold;
}
.td1 {
	padding: 5px 0;
	background-color: #DEE6FC;
	line-height: 25px;
}
.td2 {
	padding: 5px;
	background-color: #EFF3FE;
	line-height: 25px;
}
.td_over {
	padding: 5px;
	background-color: #fff;
	line-height: 25px;
	color: #203b8e;
}
.th_title {
	text-align: center;
	background-color: #fff;
	font-size: 12px;
	line-height: 13px;
	font-weight: bold;
}
-->
</style>
<script language="JavaScript">
var size = 10;
var tradeRecordList = new Array(size);


//初始化打印表格的数据
function initList(){

	for(index = 0; index<size;index++){
		var obj =  new Array();
		obj.name = "name"+index;
		obj.price= index;
		obj.content = "时间"+index;
		
		tradeRecordList[index] = obj;
		//document.getElementById("testDiv").innerHTML += "初始化"+ tradeRecordList[index].name+"="+tradeRecordList[index].price;
		
	}
}

//打印表格
function printList(){ 

	initList();

    //listDiv表示要加入的页面的位置
	var listDivObj = document.getElementById("listDiv");

	

    var tab=document.createElement("table");
    //设置table样式
    tab.width="100%";
 
   //打印表头
   var tr=tab.insertRow();
  
   tr.οnmοuseοver=function(){this.className='th_title' };
   tr.οnmοuseοut=function(){ this.className='th_title' };
   var td= tr.insertCell(0);
   td.innerHTML="序号"
   var td= tr.insertCell(1);
   td.innerHTML="商品";
   var td= tr.insertCell(2);
   td.innerHTML="售价";
   var td= tr.insertCell(3);
   td.innerText = "描述";
    
   //循环打印列表记录
   for(var i=0;i<tradeRecordList.length;i++){
       var tr=tab.insertRow();
   
       //设置tr样式
       if(i%2){//奇数行
           tr.className="td2";
           tr.οnmοuseοver=function(){this.className='td_over' };
           tr.οnmοuseοut=function(){ this.className='td2' };
       }else{//偶数行
           tr.className="td1";
           tr.οnmοuseοver=function(){this.className='td_over' };
           tr.οnmοuseοut=function(){ this.className='td1' };
       }
   
       var td= tr.insertCell(0);
       td.innerHTML=i+1;
       var td= tr.insertCell(1);
       td.innerHTML=tradeRecordList[i].name;
       var td= tr.insertCell(2);
       td.innerHTML=tradeRecordList[i].price+"元";
       var td= tr.insertCell(3);
       td.innerHTML=tradeRecordList[i].content;
  }
  document.getElementById("listDiv").appendChild(tab);
}

</script>


</head>

<body>
<p align="center" class="STYLE1">打印表格</p>
<p align="center">
  <input type="button" name="creat" value="按钮" οnclick="printList()" />

<div align="center" id="testDiv"></div>&nbsp;</p>
<p><div align="center" id="listDiv"></div>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值