在头中插入 th [英] Insert th in thead

查看:18
本文介绍了在头中插入 th的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在表的 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 Gechevgaurav.我想知道是否有任何干净的解决方案,例如使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆