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


HTML DOM Figcaption用法及代码示例


HTML DOM中的Figcaption对象用于表示HTML <figcaption>元素。 figcaption元素由getElementById()访问。

用法:

document.getElementById("ID");

其中ID代表元素ID。

范例1:

<!DOCTYPE html>  
<html>  
    <head>  
        <title> 
            HTML DOM figcaption Object 
        </title>  
          
        <style>  
            body {  
                text-align:center;  
            }  
            h1 {  
                color:green;  
            }  
        </style>  
    </head>  
      
    <body>  
        <h1>GeeksforGeeks</h1>  
          
        <h2>DOM Figcaption Object</h2>  
          
        <figure>  
            <img src=  
    "https://media.geeksforgeeks.org/wp-content/uploads/geeks-25.png"
            alt="gfglogo" style="width:50%">  
              
            <figcaption id = "GFG"> 
                GeeksforGeeks Logo 
            </figcaption>  
        </figure> 
          
        <button onclick = "Geeks()"> 
            Submit 
        </button> 
          
        <script> 
        function Geeks() { 
            var gfg = document.getElementById("GFG"); 
            gfg.style.color = "green"; 
            gfg.style.fontSize = "20px"; 
        } 
        </script>  
    </body>  
</html>                                                     

输出:
之前单击按钮:

单击按钮后:



范例2:可以使用document.createElement方法创建Figcaption对象。

<!DOCTYPE html>  
<html>  
    <head>  
        <title> 
            HTML DOM figcaption Object 
        </title>  
        <style>  
            body {  
                text-align:center;  
            }  
            h1 {  
                color:green;  
            }  
        </style>  
    </head> 
      
    <body>  
        <h1>GeeksforGeeks</h1>  
        <h2>DOM Figcaption Object</h2>  
          
        <figure>  
            <img src =  
    "https://media.geeksforgeeks.org/wp-content/uploads/geeks-25.png"
            alt = "gfglogo" style = "width:50%"> 
        </figure>  
          
        <button onclick = "Geeks()"> 
            Submit 
        </button> 
          
        <!-- script to add figcaption object -->
        <script> 
            function Geeks() { 
                var x = document.createElement("FIGCAPTION"); 
                var y = document.createTextNode("GeeksForGeeks Logo") 
                x.appendChild(y); 
                x.style.color = "green"; 
                document.body.appendChild(x); 
            } 
        </script> 
    </body>  
</html>                                            

在单击按钮之前:

单击按钮后:

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

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




相关用法


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