当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


HTML DOM Caption用法及代码示例


DOM Caption Object用于表示HTML <Caption>元素(caption元素用于指定表的标题)。 Caption元素由getElementById()访问。

属性:

  • 它具有align属性,用于设置或返回标题的对齐方式。

用法:

document.getElementById("id"); 

其中“id”是分配给Caption标签的ID。

示例1:



<!DOCTYPE html> 
<html> 
  
<head> 
    <style> 
        table, 
        th, 
        td { 
            border:1px solid black; 
        } 
    </style> 
</head> 
  
<body> 
    <h1 style="color:green;font-size:35px;"> 
      GeeksForGeeks 
    </h1> 
    <h2>DOM Caption Object</h2> 
    
    <table> 
        <caption id="GFG">Students</caption> 
        <tr> 
            <th>Firstname</th> 
            <th>Lastname</th> 
            <th>Age</th> 
        </tr> 
        <tr> 
            <td>Priya</td> 
            <td>Sharma</td> 
            <td>24</td> 
        </tr> 
        <tr> 
            <td>Arun</td> 
            <td>Singh</td> 
            <td>32</td> 
        </tr> 
        <tr> 
            <td>Sam</td> 
            <td>Watson</td> 
            <td>41</td> 
        </tr> 
    </table> 
    <br> 
    <button onclick="myGeeks()">Submit</button> 
  
    <script> 
        function myGeeks() { 
            var w = document.getElementById("GFG"); 
            w.style.color = "green"; 
            w.style.fontSize = "28px"; 
        } 
    </script> 
  
</body> 
  
</html>

输出:

在单击按钮之前:

单击按钮后:

示例-2:可以使用document.createElement方法创建标题对象。

<!DOCTYPE html> 
<html> 
  
<head> 
    <style> 
        table, 
        th, 
        td { 
            border:2px soliD green; 
        } 
    </style> 
</head> 
  
<body> 
    <h1 style="color:green;font-size:35px;"> 
      GeeksForGeeks 
    </h1> 
    <h2>DOM Caption Object</h2> 
    
    <table id="GFG"> 
        <tr> 
            <th>Firstname</th> 
            <th>Lastname</th> 
            <th>Age</th> 
        </tr> 
        <tr> 
            <td>Priya</td> 
            <td>Sharma</td> 
            <td>24</td> 
        </tr> 
        <tr> 
            <td>Arun</td> 
            <td>Singh</td> 
            <td>32</td> 
        </tr> 
        <tr> 
            <td>Sam</td> 
            <td>Watson</td> 
            <td>41</td> 
        </tr> 
    </table> 
    <br> 
    
    <button onclick="myGeeks()"> 
      Submit 
    </button> 
  
    <script> 
        function myGeeks() { 
            var cap = document.createElement("CAPTION"); 
            var txt = document.createTextNode("Students"); 
            cap.appendChild(txt); 
            cap.style.color = "red"; 
            cap.style.fontSize = "29px"; 
  
            var tab = document.getElementById("GFG") 
            tab.insertBefore(cap, tab.childNodes[0]); 
        } 
    </script> 
  
</body> 
  
</html>

输出:
在单击按钮之前:

单击按钮后:

支持的浏览器:下面列出了DOM Caption Object支持的浏览器:

  • 谷歌浏览器
  • IE浏览器
  • Firefox
  • Opera
  • Safari

相关用法


注:本文由纯净天空筛选整理自ManasChhabra2大神的英文原创作品 HTML | DOM Caption Object。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。