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


HTML DOM DT用法及代碼示例

DOM dt對象用於表示HTML <dt>元素。可以使用getElementById()方法訪問dt元素。

用法:

document.getElementById("id"); 

其中‘id’是分配給dt標簽的ID。

示例1:

<!DOCTYPE html>  
<html>  
<body>  
        <h1 style = "color:green;" >  
          GeeksForGeeks  
        </h1>  
  
        <h2>DOM DT Object</h2>  
  
        <dl> 
            <dt id="id">Sorting</dt> 
            <dd>Merge sort</dd> 
        </dl> 
          
        <button onclick="Geeks()">Click Here!</button> 
          
        <p id="demo" style="color:green"></p> 
          
        <script> 
        function Geeks() { 
            var doc = document.getElementById("id").innerHTML; 
  
            document.getElementById("demo").innerHTML = doc; 
        } 
        </script> 
</body>  
</html> 

輸出:
在點擊按鈕之前:
dt
單擊按鈕後:
dt



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

<!DOCTYPE html>  
<html>  
<body>  
        <h1 style = "color:green;" >  
          GeeksForGeeks  
        </h1>  
  
        <h2>DOM DT Object</h2>  
  
        <button onclick="Geeks()">Click Here!</button><br> 
  
        <script> 
        function Geeks() { 
            var doc = document.createElement("DL"); 
            doc.setAttribute("id", "dl"); 
            document.body.appendChild(doc); 
            
            // Creating a DT element 
            var doc1 = document.createElement("DT"); 
            var txt1 = document.createTextNode("Sorting"); 
            doc1.appendChild(txt1); 
            doc1.setAttribute("id", "dt"); 
            document.getElementById("dl").appendChild(doc1); 
            
            // Creaiting a dd element 
            var doc2 = document.createElement("DD"); 
            var txt2 = document.createTextNode("Merge sort"); 
            doc2.appendChild(txt2); 
            document.getElementById("dl").appendChild(doc2); 
        } 
        </script> 
</body>  
</html> 

輸出:
在點擊按鈕之前:
dt
單擊按鈕後:
dt

相關用法


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