HTML DOM createCaption() 方法

定义和用法

createCaption() 方法用于在表格中获取或创建 <caption> 元素。

语法

  1. tableObject.createCaption()

返回值

返回一个 HTMLElement 对象,表示该表的 <caption> 元素。如果该表格已经有了标题,则返回它。如果该表没有 <caption> 元素,则创建一个新的空 <caption> 元素,把它插入表格,并返回它。

实例

下面的例子为表格创建了一个标题:

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function createCaption()
  5. {
  6. var x=document.getElementById('myTable').createCaption()
  7. x.innerHTML="My table caption"
  8. }
  9. </script>
  10. </head>
  11. <body>
  12.  
  13. <table id="myTable" border="1">
  14. <tr>
  15. <td>Row1 cell1</td>
  16. <td>Row1 cell2</td>
  17. </tr>
  18. <tr>
  19. <td>Row2 cell1</td>
  20. <td>Row2 cell2</td>
  21. </tr>
  22. </table>
  23. <br />
  24. <input type="button" onclick="createCaption()"
  25. value="Create caption">
  26.  
  27. </body>
  28. </html>