使用Javascript创建表
我有一个JavaScript函数,它创建一个包含3行2个单元格的表。
谁能告诉我如何使用我的功能创建下表(我需要根据我的情况这样做)?
这是我的javascript和html代码,如下所示:
function tableCreate() {
//body reference
var body = document.getElementsByTagName("body")[0];
// create elements <table> and a <tbody>
var tbl = document.createElement("table");
var tblBody = document.createElement("tbody");
// cells creation
for (var j = 0; j <= 2; j++) {
// table row creation
var row = document.createElement("tr");
for (var i = 0; i < 2; i++) {
// create element <td> and text node
//Make text node the contents of <td> element
// put <td> at end of the table row
var cell = document.createElement("td");
var cellText = document.createTextNode("cell is row " + j + ", column " + i);
cell.appendChild(cellText);
row.appendChild(cell);
}
//row added to end of table body
tblBody.appendChild(row);
}
// append the <tbody> inside the <table>
tbl.appendChild(tblBody);
// put <table> in the <body>
body.appendChild(tbl);
// tbl border attribute to
tbl.setAttribute("border", "2");}<table width="100%" border="1"> <tr> <td> </td> <td> </td> </tr> <tr> <td> </td> <td rowspan="2"> </td> </tr> <tr> <td> </td> </tr></table>
森栏
炎炎设计
随时随地看视频慕课网APP