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


HTML DOM Details用法及代碼示例


DOM Details對象用於表示HTML <details>元素。 getElementById()訪問Details元素。

屬性:

  • open:詳細信息標簽具有名為‘open’的屬性,該屬性默認情況下用於顯示隱藏的信息。

用法:

document.getElementById("ID");




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

示例1:



HTML

<!DOCTYPE html>
<html>
 
<head>
    <title>DOM details Object</title>
    <style>
        h2 {
            color:green;
            font-size:35px;
        }
         
        summary {
            font-size:40px;
            color:#090;
            font-weight:bold;
        }
    </style>
</head>
 
<body>
    <center>
        <h2>DOM Details Object </h2>
 
        <!-- assigning id to details tag. -->
        <details id="GFG">
            <summary>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>
        <script>
            function myGeeks() {
                // Accessing details tag.
                var x = document.getElementById("GFG");
                 
                // Display hidden information
                // using open property. 
                x.open = true;
            }
        </script>
    </center>
</body>
 
</html>              

輸出:
在單擊按鈕之前:

單擊n按鈕後:

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

HTML

<!DOCTYPE html>
<html>
 
<head>
    <title>details tag</title>
    <style>
        h1,
        h2 {
            text-align:center;
        }
         
        h1 {
            color:green;
        }
         
        button {
            margin-left:40%;
        }
         
        details {
            font-size:30px;
            color:green;
            text-align:center;
            margin-top:30px;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <h2>DOM Details Object</h2>
    <button onclick="myGeeks()">Submit</button>
 
    <script>
        function myGeeks() {
            var g = document.createElement("DETAILS");
            g.setAttribute("id", "GFG");
            document.body.appendChild(g);
 
            var f = document.createElement("SUMMARY");
            var text = document.createTextNode("GeeksForGeeks");
            f.appendChild(text);
 
            document.getElementById("GFG").appendChild(f);
 
            var z = document.createElement("p");
            var text2 = document.createTextNode(
              "A Computer Science PortaL For Geeks");
            z.appendChild(text2);
            document.getElementById("GFG").appendChild(z);
        }
    </script>
</body>
 
</html>

輸出:
在單擊按鈕之前:

單擊按鈕後:

支持的瀏覽器:DOM Details Object支持的瀏覽器如下:

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

相關用法


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