HTMLTableElement: createCaption() Methode
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since Juli 2015.
Die HTMLTableElement.createCaption() Methode gibt das
<caption> Element zurück, das mit einem bestimmten <table> verknüpft ist.
Wenn kein <caption> Element in der Tabelle vorhanden ist, erstellt diese Methode
es und gibt es danach zurück.
Hinweis:
Wenn keine Beschriftung existiert, fügt createCaption() direkt eine neue Beschriftung
in die Tabelle ein. Die Beschriftung muss nicht separat hinzugefügt werden, wie es der Fall wäre, wenn Document.createElement() verwendet worden wäre, um das neue <caption> Element zu erstellen.
Syntax
createCaption()
Parameter
Keine.
Rückgabewert
Beispiele
Dieses Beispiel verwendet JavaScript, um einer Tabelle, die anfangs keine hat, eine Beschriftung hinzuzufügen.
HTML
<table>
<tbody>
<tr>
<td>Cell 1.1</td>
<td>Cell 1.2</td>
<td>Cell 1.3</td>
</tr>
<tr>
<td>Cell 2.1</td>
<td>Cell 2.2</td>
<td>Cell 2.3</td>
</tr>
</tbody>
</table>
JavaScript
let table = document.querySelector("table");
let caption = table.createCaption();
caption.textContent = "This caption was created by JavaScript!";
Ergebnis
Spezifikationen
| Specification |
|---|
| HTML> # dom-table-createcaption-dev> |