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


HTML DOM DD用法及代码示例


DOM DD对象用于表示HTML <DD>元素。 DD元素由getElementById()访问。

用法:

document.getElementById("ID");

其中“id”是分配给“dd”标签的ID。

示例1:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title>HTML dd Tag</title> 
    <style> 
        h1 { 
            color:green; 
        } 
          
        h1, 
        h2 { 
            text-align:center; 
        } 
          
        body { 
            width:70%; 
        } 
          
        dt { 
            color:red; 
        } 
    </style> 
</head> 
  
<body> 
    <h1>GeeksForGeeks</h1> 
    <h2>DOM <dd> Object</h2> 
    <dl> 
        <dt>Geeks Classes</dt> 
          
        <!-- Assigning id to dd -->
        <dd id="GFG"> 
          It is an extensive classroom  
          programme for enhancing DS and Algo concepts. 
        </dd> 
        <br> 
        
        <dt>Fork Python</dt> 
        <dd> 
          It is a course designed for beginners in python. 
        </dd> 
        <br> 
        
        <dt>Interview Preparation</dt> 
        <dd> 
          It is a course designed for preparation  
          of interviews in top product based companies. 
        </dd> 
    </dl> 
    
    <button onclick="myGeeks()"> 
      Submit 
    </button> 
    
    <p id="sudo"> 
    </p> 
    
    <script> 
        function myGeeks() { 
            <!-- Accessing of dd object. -->
            var g =  
             document.getElementById("GFG").innerHTML; 
             document.getElementById("sudo").innerHTML  
            = g; 
  
        } 
    </script> 
</body> 
  
</html>            

输出:



在单击按钮之前:

单击按钮后:

示例-2:可以使用document.createElement方法创建DD对象。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title>HTML dd Tag</title> 
    <style> 
        h1 { 
            color:green; 
        } 
          
        h1, 
        h2 { 
            text-align:center; 
        } 
          
        body { 
            width:70%; 
        } 
          
        dt { 
            color:red; 
        } 
    </style> 
</head> 
  
<body> 
    <center> 
        <h1>GeeksForGeeks</h1> 
        <h2>DOM <dd> Object</h2> 
  
        <button onclick="myGeeks()">Submit</button> 
  
        <dl id="GFG"> 
            <dt>Geeks Classes</dt> 
        </dl> 
        <script> 
            function myGeeks() { 
                var g = document.createElement("DD"); 
                var f = document.createTextNode( 
                    "It is an extensive classroom for"  
                  + " enhancing DS and Algo concepts."); 
                g.appendChild(f); 
  
                var w = document.getElementById("GFG"); 
                w.appendChild(g); 
            } 
        </script> 
    </center> 
</body> 
  
</html>

输出:

在单击按钮之前:

单击按钮后:

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

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

相关用法


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