3行2セルのテーブルを作成するJavaScript関数があります。
私の機能を使って、以下の表をどのように作成することができますか(誰かが私の状況でこれを行う必要があります)?JavaScriptを使用してテーブルを作成する
マイHTML:
<table width="100%" border="1">
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td rowspan="2"> </td>
</tr>
<tr>
<td> </td>
</tr>
</table>
はJavaScript:
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");
}
良い解決策が、それは良いです@Cerbrusそれはループ –
の外に変数を作成するのが最善です解決策がIE8の前に動作しません。テーブルはDOMエクスプローラに追加されますが、何とか表示されません。誰かがこれを解決する方法を知っていますか? – bdogru
@bdogru:なぜIE8をサポートする必要がありますか?それは古代です。 Google [2012年末にそのブラウザを削除](http://www.computerworld.com/article/2492571/web-apps/google-to-drop-support-for-ie8-on-nov--15.html) – Cerbrus