gpt4 book ai didi

javascript - 如何使用 Javascript 动态创建(或伪造)一个 单元格

转载 作者:行者123 更新时间:2023-11-28 13:36:46 24 4
gpt4 key购买 nike

我正在使用 Javascript 动态创建一个表格,并希望在每行的开头创建一个标题样式的单元格。所以我的问题是我该如何做到这一点,如 insertCell只创建一个正常的<td>元素而不是 <th> .

我尝试过的一些方法(通过 w3schools 的 Tryit 编辑器;我没有理由怀疑任何其他用法会有不同的行为)但没有成功:

  • 我看到了创建 <th> 的建议独立元素,然后将其添加到 <tr>元素作为一个 child 。但是,当我这样做时,它不会添加为表格单元格,即它不受表格边框的影响,不计入单元格数组(即,如果您执行 insertCell(1) ,它会插入到第一个单元格之后,而不是计算 <th> ),并且没有获得 <th> 的特殊粗体/中心格式。细胞。

  • 我尝试使用insertCell制作一个虚拟电池,然后 replaceChild拥有独立创建的细胞;这与上面的结果相同。

  • 我尝试制作 <td>单元格通过 insertCell 并简单地加粗并手动居中,但是 myCell.style.fontWeight="bold"myCell.align="center"似乎不起作用(它们只是结束函数,就像 JavaScript 中的错误命令一样),同样尝试使用 CSS 也不起作用。所以也许我只是语法不好或者什么的,但我不知道该怎么做。任何帮助将不胜感激。

尝试1:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to insert new cell(s) at the end of the table row.</p>

<table border="1">
<th>1</th>
<td>2</td>
</tr>
<tr id="myRow">
</tr>
<tr>
</table><br>

<button onclick="myFunction()">Try it</button>

<script>


function myFunction()
{
var newRow = document.getElementById("myRow");
var header=document.createElement("th").appendChild(document.createTextNode("a"));
newRow.appendChild(header);
var enablesLoc=newRow.insertCell(0).appendChild(document.createTextNode("b"));

}
</script>

</body>
</html>

结果:“1”为粗体,有边框,“2”和“b”为非粗体,有边框(它们应该是这样),“a”为非粗体,没有边框。

尝试2:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to insert new cell(s) at the end of the table row.</p>

<table border="1">
<th>1</th>
<td>2</td>
</tr>
<tr id="myRow">
</tr>
<tr>
</table><br>


<button onclick="myFunction()">Try it</button>

<script>


function myFunction()
{
var newRow = document.getElementById("myRow");
var header=newRow.insertCell(0).appendChild(document.createTextNode("a"));
header.style.fontWeight="bold";
var enablesLoc=newRow.insertCell(1).appendChild(document.createTextNode("b"));

}
</script>

</body>
</html>

结果:按钮添加单元格“a”(未加粗),但不添加单元格“b”。

最佳答案

以下代码显示了您可以使用的所有内容:

function addTable(){
var table = document.createElement("table");
var tr = document.createElement("tr");
var th = document.createElement("th");
var td = document.createElement("td");
td.innerText = "im a td";
th.innerText = "im a th";
tr.appendChild(th);
tr.appendChild(td);
table.appendChild(tr);
var out = document.getElementById("out");
out.appendChild(table);
}

您必须调用该函数和一个 id 为 <div id=out>...</div> 的 div必须在文档中。 免责声明:我只在 Chrome 中测试了此代码

更新以解决您的观点

您写了I've seen a suggestion to create the <th> element independently and then add it to the <tr> element as a child. When I do this

  • it is not added as a table cell你这是什么意思以及你使用什么命令,
  • is not affected by the table border原因可能是因为它不包含文本,
  • does not count toward the array of cells (i.e. if you do insertCell(1)我也不明白这一点。根据insertCell上的规范它插入一个空的 td并返回一个引用。所以 insertCell 没有数组,如果你尝试 var table = document.getElementById("myTable")然后table.rows[0].cells.length它返回包括 th 在内的单元格数量-细胞。
  • it inserts after the first cell not counting the th根据我的测试,至少在 Chrome 中情况并非如此;这取决于您如何调用该方法:table.rows[1].insertCell(-1);在第二行(从零开始的数组)末尾添加一个单元格和 table.rows[2].insertCell(1);如果您使用 table.rows[3].insertCell(0);,则在第三行的位置 2 上添加一个单元格(同样从零开始)单元格插入到第四个单元格中。行在开头,
  • and does not get the special bold/center format for a th cell我也不是这样的

免责声明:我只在 Chrome 中测试了此代码

html

<button onclick="addRow()">add rows</button><br />
<button onclick="addColumn()">add column</button>

<table border="1" id="myTable">
<tr>
<th>1</th>
<td>2</td>
</tr>
</table>

还有 JavaScript

function addRow()
{
var table = document.getElementById("myTable");
var tr = document.createElement("tr");
var th = document.createElement("th");
var td = document.createElement("td");
td.innerText = "im a td";
th.innerText = "im a th";
tr.appendChild(th);
tr.appendChild(td);
table.appendChild(tr);
}

function addColumn(){
var table = document.getElementById("myTable");
var rows = table.rows;
console.log("rows", rows);

for (var i = 0; i < rows.length; ++i) {
// td = rows[i].cells;
var td = document.createElement("td");
td.innerText = i;
rows[i].appendChild(td);
}
}

基于DOM Level 2 HTML 您不能使用insertCell因为它Insert[s] an empty TD cell into this row 。但你想添加一个th根据DOM Level 3 Core 您可以使用appendChild因为它adds the node newChild to the end of the list of children of this node. If the newChild is already in the tree, it is first removed.

因此,您必须以正确的顺序创建元素:创建行,将第一个单元格添加为 th然后添加其他单元格为 td通过使用追加子项。

您可以看看a demo of the above code

关于javascript - 如何使用 Javascript 动态创建(或伪造)一个 <th> 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20865146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com