js代码
(function() {
window.Table = function(obj) {
this.nodeElement = obj.el || document.body;
this.appWidth = this.nodeElement.clientWidth;
this.cellWidth = 0;
}
Table.prototype.createTable = function(obj) {
if(!obj) {
throw "Please set the number of rows or columns,arg[obj]";
} else {
this.cellWidth = this.appWidth/obj.col;
this.Ocss();
for(var i = 0; i < obj.row; i++) {
var rowEle = document.createElement("div");
this.nodeElement.appendChild(rowEle);
rowEle.className = "t-row";
if(i % 2 == 0) {
rowEle.className += " t-double";
}
for(var j = 0; j < obj.col; j++) {
var colEle = document.createElement("div");
rowEle.appendChild(colEle);
colEle.className = "t-col";
}
}
}
}
Table.prototype.Ocss = function() {
var css = document.getElementsByTagName("style")[0];
if(!css) {
css = document.createElement("style");
document.head.appendChild(css);
}
css.innerText += ".t-row{width:100%;height:30px;background:#ecffff;border-bottom:1px solid red}.t-double{background:#a6ffff} .t-col{box-sizing:border-box;float:left;width:"+this.cellWidth+"px;height:100%;border-left:1px solid #fff}";
}
})()
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>JS表格插件</title>
</head>
<style>
*{margin: 0;padding: 0;}
#app{
width: 800px;
min-height: 100px;
border: 1px solid red;
margin: 30px auto;
}
</style>
<body>
<div id="app"></div>
</body>
<script type="text/javascript" src="js/chart.js"></script>
<script type="text/javascript">
var table = new Table({
el:app
})
table.createTable({
row:313,
col:11,
});
</script>
</html>