當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。