- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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
通过使用追加子项。
关于javascript - 如何使用 Javascript 动态创建(或伪造)一个 <th> 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20865146/
我有一个 html 格式的表单: 我需要得到 JavaScript在value input 字段执行,但只能通过表单的 submit .原因是页面是一个模板所以我不控制它(不能有
我管理的论坛是托管软件,因此我无法访问源代码,我只能向页面添加 JavaScript 来实现我需要完成的任务。 我正在尝试用超链接替换所有页面上某些文本关键字的第一个实例。我还根据国家/地区代码对这些
我正在使用 JS 打开新页面并将 HTML 代码写入其中,但是当我尝试使用 document.write() 在新页面中编写 JS 时功能不起作用。显然,一旦看到 ,主 JS 就会关闭。用于即将打开的
提问不是为了解决问题,提问是为了更好地理解系统 专家!我知道每当你将 javascript 代码输入 javascript 引擎时,它会立即由 javascript 引擎执行。由于没有看过Engi
我在一个文件夹中有两个 javascript 文件。我想将一个变量的 javascript 文件传递到另一个。我应该使用什么程序? 最佳答案 window.postMessage用于跨文档消息。使
我有一个练习,我需要输入两个输入并检查它们是否都等于一个。 如果是 console.log 正则 console.log false 我试过这样的事情: function isPositive(fir
我正在做一个Web应用程序,计划允许其他网站(客户端)在其页面上嵌入以下javascript: 我的网络应用程序位于 http://example.org 。 我不能假设客户端网站的页面有 JQue
目前我正在使用三个外部 JS 文件。 我喜欢将所有三个 JS 文件合而为一。 尽一切可能。我创建 aio.js 并在 aio.js 中 src="https://code.jquery.com/
我有例如像这样的数组: var myArray = []; var item1 = { start: '08:00', end: '09:30' } var item2 = {
所以我正在制作一个 Chrome 扩展,它使用我制作的一些 TamperMonkey 脚本。我想要一个“主”javascript 文件,您可以在其中包含并执行其他脚本。我很擅长使用以下行将其他 jav
我有 A、B html 和 A、B javascript 文件。 并且,如何将 A JavaScript 中使用的全局变量直接移动到 B JavaScript 中? 示例 JavaScript) va
我需要将以下整个代码放入名为 activate.js 的 JavaScript 中。你能告诉我怎么做吗? var int = new int({ seconds: 30, mark
我已经为我的 .net Web 应用程序创建了母版页 EXAMPLE1.Master。他们的 I 将值存储在 JavaScript 变量中。我想在另一个 JS 文件中检索该变量。 示例1.大师:-
是否有任何库可以用来转换这样的代码: function () { var a = 1; } 像这样的代码: function () { var a = 1; } 在我的浏览器中。因为我在 Gi
我收到语法缺失 ) 错误 $(document).ready(function changeText() { var p = document.getElementById('bidp
我正在制作进度条。它有一个标签。我想调整某个脚本完成的标签。在找到可能的解决方案的一些答案后,我想出了以下脚本。第一个启动并按预期工作。然而,第二个却没有。它出什么问题了?代码如下: HTML:
这里有一个很简单的问题,我简单的头脑无法回答:为什么我在外部库中加载时,下面的匿名和onload函数没有运行?我错过了一些非常非常基本的东西。 Library.js 只有一行:console.log(
我知道 javascript 是一种客户端语言,但如果实际代码中嵌入的 javascript 代码以某种方式与在控制台上运行的代码不同,我会尝试找到答案。让我用一个例子来解释它: 我想创建一个像 Mi
我如何将这个内联 javascript 更改为 Unobtrusive JavaScript? 谢谢! 感谢您的回答,但它不起作用。我的代码是: PHP js文件 document.getElem
我正在寻找将简单的 JavaScript 对象“转储”到动态生成的 JavaScript 源代码中的最优雅的方法。 目的:假设我们有 node.js 服务器生成 HTML。我们在服务器端有一个对象x。
我是一名优秀的程序员,十分优秀!