我是一名初学者,正在为类(class)做作业。作业要求我们创建一个包含 html、meta、title 和 body 标签的空白 html 文件。然后,它指示我们使用 JavaScript 代码填充此 html 文件,其中包含以下详细信息:
一个 4x4 表格,其中标题行算作 4 行之一。
4 个按钮,标记为上/下/左/右。
第五个按钮标记为“标记单元格”。
...
其余的细节我就不讲了。这是第一部分,我遇到了麻烦,因为我已经创建了其他部分。
我编写了以下代码来解决第一部分。
var table = document.createElement('table');
var header = document.createThead();
var frstRow = document.createElement('tr');
var databits = document.createElement('td');
frstRow.appendChild(databits);
header.appendChild(frstRow);
table.appendChild(header);
----------------------Everything Below this line works by itself...but it is missing a header
for (var i = 1; i < 4; i++){
var tr = document.createElement('tr');
var td1 = document.createElement('td');
var td2 = document.createElement('td');
var td3 = document.createElement('td');
var td4 = document.createElement('td');
table.style.border="1px solid black";
td1.style.border="1px solid black";
td2.style.border="1px solid black";
td3.style.border="1px solid black";
td4.style.border="1px solid black";
var text1 = document.createTextNode(i);
var text2 = document.createTextNode(i);
var text3 = document.createTextNode(i);
var text4 = document.createTextNode(i);
td1.appendChild(text1);
td2.appendChild(text2);
td3.appendChild(text3);
td4.appendChild(text4);
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
tr.appendChild(td4);
table.appendChild(tr);
}
document.body.appendChild(table);
如果我在我画的线下做所有事情,它 works 。它显示为 3rx4c 表。我想在该表的顶部添加一个 Th,因此我认为可以将代码添加到表头的顶部以及该表头的相应行和 td。当我这样做时,我显然错了,nothing displays !
我做错了什么?
最佳答案
以下不是最优雅的方法,也绝对不是最快的方法,但我认为它非常简洁地显示了必要的步骤。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Put a title here. No really, do it!</title>
<script type="text/javascript">
function makeTable(){
var table = document.createElement('table');
var header = document.createElement('th');
var frstRow = document.createElement('tr');
var databits = document.createElement('td');
var text;
table.style.border = "1px solid black";
header.style.border = "1px solid green";
databits.style.border = "1px solid red";
// build the header row
for(var i = 0;i < 4; i++){
frstRow.appendChild(header.cloneNode());
// fill the individual header cells
frstRow.lastChild.textContent = "header " + (i+1);
}
// add it to the table
table.appendChild(frstRow);
// reset frstRow
frstRow = document.createElement('tr');
// make a normal row
for(var i = 0;i < 4; i++){
frstRow.appendChild(databits.cloneNode());
// fill the individual normal cells
frstRow.lastChild.textContent = "column " + (i+1);
}
for(var i = 0;i < 3; i++){
// take the text content of the first cell in this row
text = frstRow.firstChild.textContent;
// append one row (cloneNode(true) copies everything in it, too)
table.appendChild(frstRow.cloneNode(true));
// set the first cell of the last row to be an indicator for the row#
table.lastChild.firstChild.textContent = "Row " + (i+2) + " " + text;
}
// put the table in the body
document.body.appendChild(table);
}
</script>
</head>
<body onload="makeTable()">
</body>
</html>
关于javascript - 添加元素到 DOM 树,帮助在 Javascript 中创建表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33381495/