HTMLTableElement.insertRow()
method inserts a new row (
<tr>
) in a given
<table>
, and returns a reference to the new row.
If a table has multiple
<tbody>
elements, by default, the new row is inserted into the last
<tbody>
. To insert the row into a specific
<tbody>
:
let specific_tbody = document.getElementById(tbody_id); let row = specific_tbody.insertRow(index)
注意:
insertRow()
inserts the row directly into the table. The row does not need to be appended separately as would be the case if
Document.createElement()
had been used to create the new
<tr>
元素。
var newRow = HTMLTableElement.insertRow(index);
HTMLTableElement
is a reference to an HTML
<table>
元素。
index
可选
index
is
-1
or equal to the number of rows, the row is appended as the last row. If
index
is greater than the number of rows, an
IndexSizeError
exception will result. If
index
is omitted it defaults to
-1
.
newRow
是
HTMLTableRowElement
that references the new row.
此范例使用
insertRow(-1)
to append a new row to a table.
We then use
HTMLTableRowElement.insertCell()
to insert a new cell in the new row. (To be valid HTML, a
<tr>
must have at least one
<td>
element.) Finally, we add some text to the cell using
Document.createTextNode()
and
Node.appendChild()
.
<table id="my-table"> <tr><td>Row 1</td></tr> <tr><td>Row 2</td></tr> <tr><td>Row 3</td></tr> </table>
function addRow(tableID) {
// Get a reference to the table
let tableRef = document.getElementById(tableID);
// Insert a row at the end of the table
let newRow = tableRef.insertRow(-1);
// Insert a cell in the row at index 0
let newCell = newRow.insertCell(0);
// Append a text node to the cell
let newText = document.createTextNode('New bottom row');
newCell.appendChild(newText);
}
// Call addRow() with the table's ID
addRow('my-table');
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'HTMLTableElement.insertRow()' in that specification. |
实时标准 | |
|
DOM (文档对象模型) 2 级 HTML 规范
The definition of 'HTMLTableElement.insertRow()' in that specification. |
过时 | Specifies in more detail where the row is inserted. |
|
DOM (文档对象模型) 1 级规范
The definition of 'HTMLTableElement.insertRow()' in that specification. |
过时 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
insertRow
|
Chrome 4 | Edge 12 |
Firefox
3
注意事项
|
IE 5.5 | Opera 10 | Safari 4 | WebView Android ≤37 | Chrome Android 18 |
Firefox Android
4
注意事项
|
Opera Android 10.1 | Safari iOS 3.2 | Samsung Internet Android 1.0 |
完整支持
见实现注意事项。
HTMLTableRowElement.insertCell()
HTMLTableRowElement
HTMLTableElement
BeforeUnloadEvent
DOMStringMap
ErrorEvent
GlobalEventHandlers
HTMLAnchorElement
HTMLAreaElement
HTMLAudioElement
HTMLBRElement
HTMLBaseElement
HTMLBaseFontElement
HTMLBodyElement
HTMLButtonElement
HTMLCanvasElement
HTMLContentElement
HTMLDListElement
HTMLDataElement
HTMLDataListElement
HTMLDialogElement
HTMLDivElement
HTMLDocument
HTMLElement
HTMLEmbedElement
HTMLFieldSetElement
HTMLFormControlsCollection
HTMLFormElement
HTMLFrameSetElement
HTMLHRElement
HTMLHeadElement
HTMLHeadingElement
HTMLHtmlElement
HTMLIFrameElement
HTMLImageElement
HTMLInputElement
HTMLIsIndexElement
HTMLKeygenElement
HTMLLIElement
HTMLLabelElement
HTMLLegendElement
HTMLLinkElement
HTMLMapElement
HTMLMediaElement
HTMLMetaElement
HTMLMeterElement
HTMLModElement
HTMLOListElement
HTMLObjectElement
HTMLOptGroupElement
HTMLOptionElement
HTMLOptionsCollection
HTMLOutputElement
HTMLParagraphElement
HTMLParamElement
HTMLPictureElement
HTMLPreElement
HTMLProgressElement
HTMLQuoteElement
HTMLScriptElement
HTMLSelectElement
HTMLShadowElement
HTMLSourceElement
HTMLSpanElement
HTMLStyleElement
HTMLTableCaptionElement
HTMLTableCellElement
HTMLTableColElement
HTMLTableDataCellElement
HTMLTableHeaderCellElement
HTMLTableRowElement
HTMLTableSectionElement
HTMLTemplateElement
HTMLTextAreaElement
HTMLTimeElement
HTMLTitleElement
HTMLTrackElement
HTMLUListElement
HTMLUnknownElement
HTMLVideoElement
HashChangeEvent
历史
ImageData
定位
MessageChannel
MessageEvent
MessagePort
Navigator
NavigatorGeolocation
NavigatorID
NavigatorLanguage
NavigatorOnLine
NavigatorPlugins
PageTransitionEvent
Plugin
PluginArray
PopStateEvent
PortCollection
PromiseRejectionEvent
RadioNodeList
Transferable
ValidityState
Window
WindowBase64
WindowEventHandlers
WindowTimers