あなたはそれのプロトタイプを変更することにより、ネイティブ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);
した後、任意の新しいHTMLTableRowElements(「TD」タグ)親がthead要素タグであるかどうかをチェックします。その場合は、insertCellと同じ機能を行いますが、代わりにthタグを使用します。そうでない場合は、元のinsertCell機能を使用します。
it is generally not recommended to extend the native objects.
は無効であり、さまざまなブラウザでは、不要な結果をもたらす可能性がthead' 'th'として直接の子'、助言されることに注意してください。有効なレイアウトは次のようなものです: 'table-> thead-> tr-> th' –
申し訳ありません、私の悪い、私はthの中のthを意味しました。 –
私は 'thead'の中で' tr'を使って自分の答えを修正しました:-) –