HTMLTableRowElement.insertCell()
method inserts a new cell (
<td>
) into a table row (
<tr>
) and returns a reference to the cell.
注意:
insertCell()
inserts the cell directly into the row. The cell does not need to be appended separately with
Node.appendChild()
as would be the case if
Document.createElement()
had been used to create the new
<td>
元素。
You can not use
insertCell()
to create a new
<th>
element though.
var newCell = HTMLTableRowElement.insertCell(index);
HTMLTableRowElement
is a reference to an HTML
<tr>
元素。
index
可选
index
is the cell index of the new cell. If
index
is
-1
or equal to the number of cells, the cell is appended as the last cell in the row. If
index
is greater than the number of cells, an
IndexSizeError
exception will result. If
index
is omitted it defaults to
-1
.
newCell
是
HTMLTableCellElement
that references the new cell.
此范例使用
HTMLTableElement.insertRow()
to append a new row to a table.
We then use
insertCell(0)
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');
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
insertCell
|
Chrome Yes | Edge 12 | Firefox 1 | IE Yes | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 4 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
支持
-1
as an index argument
|
Chrome Yes | Edge 12 | Firefox 20 | IE Yes | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 20 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
| Index parameter is optional | Chrome Yes | Edge 12 | Firefox 20 | IE Yes | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 20 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
HTMLTableElement.insertRow()
HTMLTableCellElement
HTMLTableRowElement
insertCell()
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
HTMLTableElement
HTMLTableHeaderCellElement
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