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 可选
The row index of the new row. If 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() .

HTML

<table id="my-table">
  <tr><td>Row 1</td></tr>
  <tr><td>Row 2</td></tr>
  <tr><td>Row 3</td></tr>
</table>
					

JavaScript

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.
过时 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
insertRow Chrome 4 Edge 12 Firefox 3 注意事项
3 注意事项
Starting with Firefox 20, the index argument has been made optional and defaults to -1 as per HTML specification.
IE 5.5 Opera 10 Safari 4 WebView Android ≤37 Chrome Android 18 Firefox Android 4 注意事项
4 注意事项
Starting with Firefox 20, the index argument has been made optional and defaults to -1 as per HTML specification.
Opera Android 10.1 Safari iOS 3.2 Samsung Internet Android 1.0

图例

完整支持

完整支持

见实现注意事项。

另请参阅

元数据

  • 最后修改:
  1. HTMLTableElement
  2. 特性
    1. align
    2. bgColor
    3. border
    4. caption
    5. cellPadding
    6. cellSpacing
    7. frame
    8. rows
    9. rules
    10. summary
    11. tBodies
    12. tFoot
    13. tHead
    14. width
  3. 方法
    1. createCaption()
    2. createTFoot()
    3. createTHead()
    4. deleteCaption()
    5. deleteRow()
    6. deleteTFoot()
    7. deleteTHead()
    8. insertRow()
  4. 继承:
    1. HTMLElement
    2. 元素
    3. 节点
    4. EventTarget
  5. HTML DOM 相关页面
    1. BeforeUnloadEvent
    2. DOMStringMap
    3. ErrorEvent
    4. GlobalEventHandlers
    5. HTMLAnchorElement
    6. HTMLAreaElement
    7. HTMLAudioElement
    8. HTMLBRElement
    9. HTMLBaseElement
    10. HTMLBaseFontElement
    11. HTMLBodyElement
    12. HTMLButtonElement
    13. HTMLCanvasElement
    14. HTMLContentElement
    15. HTMLDListElement
    16. HTMLDataElement
    17. HTMLDataListElement
    18. HTMLDialogElement
    19. HTMLDivElement
    20. HTMLDocument
    21. HTMLElement
    22. HTMLEmbedElement
    23. HTMLFieldSetElement
    24. HTMLFormControlsCollection
    25. HTMLFormElement
    26. HTMLFrameSetElement
    27. HTMLHRElement
    28. HTMLHeadElement
    29. HTMLHeadingElement
    30. HTMLHtmlElement
    31. HTMLIFrameElement
    32. HTMLImageElement
    33. HTMLInputElement
    34. HTMLIsIndexElement
    35. HTMLKeygenElement
    36. HTMLLIElement
    37. HTMLLabelElement
    38. HTMLLegendElement
    39. HTMLLinkElement
    40. HTMLMapElement
    41. HTMLMediaElement
    42. HTMLMetaElement
    43. HTMLMeterElement
    44. HTMLModElement
    45. HTMLOListElement
    46. HTMLObjectElement
    47. HTMLOptGroupElement
    48. HTMLOptionElement
    49. HTMLOptionsCollection
    50. HTMLOutputElement
    51. HTMLParagraphElement
    52. HTMLParamElement
    53. HTMLPictureElement
    54. HTMLPreElement
    55. HTMLProgressElement
    56. HTMLQuoteElement
    57. HTMLScriptElement
    58. HTMLSelectElement
    59. HTMLShadowElement
    60. HTMLSourceElement
    61. HTMLSpanElement
    62. HTMLStyleElement
    63. HTMLTableCaptionElement
    64. HTMLTableCellElement
    65. HTMLTableColElement
    66. HTMLTableDataCellElement
    67. HTMLTableHeaderCellElement
    68. HTMLTableRowElement
    69. HTMLTableSectionElement
    70. HTMLTemplateElement
    71. HTMLTextAreaElement
    72. HTMLTimeElement
    73. HTMLTitleElement
    74. HTMLTrackElement
    75. HTMLUListElement
    76. HTMLUnknownElement
    77. HTMLVideoElement
    78. HashChangeEvent
    79. 历史
    80. ImageData
    81. 定位
    82. MessageChannel
    83. MessageEvent
    84. MessagePort
    85. Navigator
    86. NavigatorGeolocation
    87. NavigatorID
    88. NavigatorLanguage
    89. NavigatorOnLine
    90. NavigatorPlugins
    91. PageTransitionEvent
    92. Plugin
    93. PluginArray
    94. PopStateEvent
    95. PortCollection
    96. PromiseRejectionEvent
    97. RadioNodeList
    98. Transferable
    99. ValidityState
    100. Window
    101. WindowBase64
    102. WindowEventHandlers
    103. WindowTimers