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


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。非经特殊声明,原始代码版权归原作者所有,本译文的传播和使用请遵循“署名-相同方式共享 4.0 国际 (CC BY-SA 4.0)”协议。