Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Tạo caption cho bảng với JavaScript DOM

Để tạo tiêu đề (caption) cho bảng, sử dụng phương thức createCaption() của DOM.

Ví dụ

Mã sau thêm caption vào bảng khi nhấn nút:

<!DOCTYPE html>
<html>
  <head>
    <script>
      function captionFunc(x) {
        document.getElementById(x).createCaption().innerHTML = "Demo Caption";
      }
    </script>
  </head>
  <body>
    <table style="border:2px solid black" id="newtable">
      <tr><td>One</td><td>Two</td></tr>
      <tr><td>Three</td><td>Four</td></tr>
      <tr><td>Five</td><td>Six</td></tr>
    </table>
    <p>
      <input type="button" onclick="captionFunc('newtable')" value="Display Table Caption">
    </p>
  </body>
</html>