HTMLTableElement: createCaption() 方法
HTMLTableElement.createCaption() 方法返回與給定 <table> 元素關聯的 <caption> 元素。如果表中不存在 <caption> 元素,此方法會建立它,然後返回它。
注意: 如果不存在標題,createCaption() 會將新標題直接插入表中。與使用 Document.createElement() 建立新的 <caption> 元素時需要單獨新增標題不同,這裡不需要單獨新增。
語法
js
createCaption()
引數
無。
返回值
HTMLTableCaptionElement
示例
本示例使用 JavaScript 為一個最初沒有標題的表格新增標題。
HTML
html
<table>
<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>
</table>
JavaScript
js
let table = document.querySelector("table");
let caption = table.createCaption();
caption.textContent = "This caption was created by JavaScript!";
結果
規範
| 規範 |
|---|
| HTML # dom-table-createcaption-dev |
瀏覽器相容性
載入中…