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


HTML DOM Underline用法及代碼示例


DOM下劃線對象用於表示HTML <u>元素。下劃線元素由getElementById()訪問。

用法:

document.getElementById("id"); 

其中‘id’是分配給<u>標記的ID。

範例1:

<!DOCTYPE html>  
<html>  
    <head> 
        <title> 
            HTML DOM underline Object 
        </title> 
    </head> 
      
    <body style = "text-align:center;">  
      
        <h1 style = "color:green;" >  
            GeeksForGeeks  
        </h1>  
          
        <h2>DOM underline Object</h2>  
  
        <p><u id = "u">GeeksforGeeks!</u> A computer 
        science portal for geeks.</p> 
          
        <button onclick = "Geeks()"> 
            Click Here! 
        </button> 
          
        <script> 
            function Geeks() { 
                var txt = document.getElementById("u"); 
                txt.style.color = "green"; 
            } 
        </script> 
    </body>  
</html>                    

輸出:
在單擊按鈕之前:
underline
單擊按鈕後:
underline



範例2:可以使用document.createElement方法創建下劃線對象。

<!DOCTYPE html>  
<html>  
    <head> 
        <title> 
            HTML DOM underline Object 
        </title> 
    </head> 
      
    <body style = "text-align:center"> 
           
        <h1 style = "color:green;" >  
            GeeksForGeeks  
        </h1>  
          
        <h2>DOM underline Object</h2>  
  
        <button onclick = "Geeks()"> 
            Click Here! 
        </button> 
          
        <p id = "p"></p> 
          
        <script> 
            function Geeks() { 
                var txt = document.createElement("U"); 
                var t = document.createTextNode("An underlined text."); 
                txt.appendChild(t); 
                document.getElementById("p").appendChild(txt); 
            } 
        </script> 
    </body>  
</html>                    

輸出:
在單擊按鈕之前:
underline
單擊按鈕後:
underline




相關用法


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