Table createCaption() 方法

定义和用法

createCaption() 方法创建空的 <caption> element 并将其添加到表中。

注释:如果表中已经存在 <caption> 元素,则 createCaption() 方法返回现有元素,并且不会创建新元素。

提示:如需从表中删除 <caption> 元素,请使用 deleteCaption() 方法.

另请参阅:

HTML 参考手册:HTML <caption> tag

实例

例子 1

为表格创建带文本的 <caption> 元素:

var table = document.getElementById("myTable").createCaption();
table.innerHTML = "<b>My table caption</b>";

Try it yourself

例子 2

创建和删除 <caption> 元素:

function myCreateFunction() {
  var table = document.getElementById("myTable").createCaption();
  table.innerHTML = "<b>My table caption</b>";
}
function myDeleteFunction() {
  document.getElementById("myTable").deleteCaption();
}

Try it yourself

Syntax

tableObject.createCaption()

Parameters

None.

Technical details

Return value: Newly created (or existing)<caption> element.

Browser support

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support