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


HTML DOM Summary用法及代碼示例


DOM摘要對象用於表示HTML <summary>元素。 “摘要”元素由getElementById()訪問。

用法:

document.getElementById("ID"); 

其中“id”是分配給“summary”標簽的ID。

示例1:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title>DOM Summary Object</title> 
    <style> 
        h2 { 
            color:green; 
            font-size:35px; 
        } 
          
        summary { 
            font-size:40px; 
            color:#090; 
            font-weight:bold; 
        } 
    </style> 
</head> 
  
<body> 
    <center> 
        <h2>DOM Summary Object </h2> 
  
        <details> 
          <!-- assigning id to summary tag. -->
          <summary id="GFG">GeeksforGeeks</summary> 
          <p>A computer science portal for geeks</p> 
          <div>It is a computer science portal  
            where you can learn programming.</div> 
        </details> 
  
        <br> 
        <button onclick="myGeeks()">Submit</button> 
        <p id="sudo"></p> 
        <script> 
            function myGeeks() { 
                // Accessing summary tag  
                var x = document.getElementById("GFG").innerHTML; 
                // display text content present in summary tag 
                document.getElementById("sudo").innerHTML = x; 
            } 
        </script> 
    </center> 
</body> 
</html>             

輸出:
在單擊按鈕之前:



單擊按鈕後:

示例-2:可以使用document.createElement方法創建摘要對象。

<!DOCTYPE html>  
<html>  
  
<head>  
    <title>DOM Summary Object</title>  
    <style>  
       h1  { 
         color:green; 
         font-size:35px; 
         } 
        h2 {  
            color:green;  
            font-size:35px;  
        }  
          
        summary {  
            font-size:40px;  
            color:#090;  
            font-weight:bold;  
        }  
    </style>  
</head>  
  
<body>  
    <center>  
       <h1>GeeksForGeeks</h1> 
        <h2>DOM Summary Object </h2> 
        <br>  
        <button onclick="myGeeks()">Submit</button>  
        <script>  
            function myGeeks() {  
              
        //  Creating details object.     
        var g = document.createElement("DETAILS"); 
        document.body.appendChild(g); 
  
  //  Creating summary object. 
  var summary = document.createElement("SUMMARY"); 
  var text1 = document.createTextNode("GeeksForGeeks"); 
  summary.appendChild(text1); 
  
   var para = document.createElement("P"); 
   var text2 = document.createTextNode( 
   "A computer science portal for geeks"); 
  para.appendChild(text2); 
  
  g.appendChild(summary); 
  g.appendChild(para);          
            }  
        </script>  
    </center>  
</body>  
  
</html>             

輸出:

在單擊按鈕之前:

單擊按鈕後:

支持的瀏覽器:下麵列出了DOM摘要對象支持的瀏覽器:

  • 穀歌瀏覽器
  • IE瀏覽器
  • Firefox
  • Safari
  • Opera

相關用法


注:本文由純淨天空篩選整理自ManasChhabra2大神的英文原創作品 HTML | DOM Summary Object。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。