简体   繁体   English

在thead中插入

[英]Insert th in thead

I want to insert a th tag inside tr of thead element of a table. 我想在表的thead元素的tr中插入一个th标签。 I am using insertCell method of row object created under table.tHead , which is actually inserting td . 我使用insertCell下创建行对象的方法table.tHead ,该实际插入td Is there any JavaScript solution without using any JS library? 有没有使用任何JS库的JavaScript解决方案?

Update Currently I am using something same as solution provided by Minko Gechev and gaurav . 更新目前我正在使用与Minko Gechevgaurav提供的解决方案相同的东西。 I want to know if there is any clean solution like using insertCell ? 我想知道是否有任何干净的解决方案,比如使用insertCell

You can also use the insertCell method as originally requested. 您还可以按原始请求使用insertCell方法。 You just have to change the outerHTML to overwrite the <td> created by the insertCell method: 您只需更改outerHTML以覆盖insertCell方法创建的<td>

var table = document.createElement("TABLE")
var row   = table.insertRow(0);
    row.insertCell(0).outerHTML = "<th>First</th>";  // rather than innerHTML

To match the example given: 为了匹配给出的例子:

HTML HTML

<table id="table">
  <thead>
    <tr>
      <th>First</th>
    </tr>
  <thead>
</table>

Javascript 使用Javascript

var tr = document.getElementById('table').tHead.children[0];
    tr.insertCell(1).outerHTML = "<th>Second</th>"  // some browsers require the index parm -- 1

You can do it with vanilla JavaScript. 你可以用vanilla JavaScript做到这一点。 Try this: 试试这个:

HTML HTML

<table id="table">
  <thead>
    <tr>
      <th>First</th>
    </tr>
  <thead>
</table>

JavaScript JavaScript的

var tr = document.getElementById('table').tHead.children[0],
    th = document.createElement('th');
th.innerHTML = "Second";
tr.appendChild(th);

Here is an example http://codepen.io/anon/pen/Bgwuf 这是一个例子http://codepen.io/anon/pen/Bgwuf

Use table.tHead.children[0].appendChild(document.createElement("th")) method instead. 请改用table.tHead.children[0].appendChild(document.createElement("th"))方法。 Basically you have to create a th at runtime and insert it into your thead. 基本上,你必须创建一个th在运行时将其插入到你的THEAD。

You can add this functionality to the native HTMLTableRowElement by changing it's prototype: 您可以通过更改原型来将此功能添加到本机HTMLTableRowElement:

HTMLTableRowElement.prototype.insertCell = (function(oldInsertCell) {
    return function(index) {
        if (this.parentElement.tagName.toUpperCase() == "THEAD") {
            if (index < -1 || index > this.cells.length) {
                // This case is suppose to throw a DOMException, but we can't construct one
                // Just let the real function do it.
            } else {
                let th = document.createElement("TH");
                if (arguments.length == 0 || index == -1 || index == this.cells.length) {
                    return this.appendChild(th);
                } else {
                    return this.insertBefore(th, this.children[index]);
                }
            }
        }
        return oldInsertCell.apply(this, arguments);
    }
})(HTMLTableRowElement.prototype.insertCell);

After this code is run, any new HTMLTableRowElements ("td" tags) will check to see if the parent is a thead tag. 运行此代码后,任何新的HTMLTableRowElements(“td”标记)都将检查父级是否为thead标记。 If so, it will do the same functionality as insertCell, but using a th tag instead. 如果是这样,它将执行与insertCell相同的功能,但使用th标签。 If not, it will just use the original insertCell functionality. 如果没有,它将只使用原始的insertCell功能。

document.querySelector("thead > tr").insertCell(); // inserts a th into a tr inside a thead
document.querySelector(":not(thead) > tr").insertCell(); // inserts a td into a tr not inside a thead

Note that it is generally not recommended to extend the native objects. 请注意, 通常建议扩展本机对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM