在头中插入 th [英] Insert th in thead
问题描述
我想在表的 thead
元素的 tr
中插入一个 th
标记.我正在使用在table.tHead
下创建的行对象的insertCell
方法,它实际上是插入td
.有没有不使用任何 JS 库的 JavaScript 解决方案?
I want to insert a th
tag inside tr
of thead
element of a table. I am using insertCell
method of row object created under table.tHead
, which is actually inserting td
. Is there any JavaScript solution without using any JS library?
更新目前我正在使用与 Minko Gechev 和 gaurav.我想知道是否有任何干净的解决方案,例如使用 insertCell
?
Update
Currently I am using something same as solution provided by Minko Gechev and gaurav. I want to know if there is any clean solution like using insertCell
?
推荐答案
您可以使用 vanilla JavaScript 来完成.试试这个:
You can do it with vanilla JavaScript. Try this:
HTML
<table id="table">
<thead>
<tr>
<th>First</th>
</tr>
<thead>
</table>
JavaScript
var tr = document.getElementById('table').tHead.children[0],
th = document.createElement('th');
th.innerHTML = "Second";
tr.appendChild(th);
这是一个例子 http://codepen.io/anon/pen/Bgwuf
这篇关于在头中插入 th的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!