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


HTML DOM Abbreviation用法及代碼示例


DOM縮寫對象用於表示HTML <abbr>元素。縮寫元素由getElementById()訪問。

範例1:

<!DOCTYPE html> 
<html> 
    <head> 
        <title> 
            HTML DOM Abbreviation Object 
        </title> 
          
        <script> 
            function myGeeks() { 
                var abbr = document.getElementById("sudo").title; 
                document.getElementById("geeks").innerHTML = abbr; 
            } 
        </script> 
    </head> 
      
    <body> 
        <h2>DOM Abbreviation Object</h2> 
          
        <abbr id = "sudo" title="GeeksforGeeks"> 
            GFG 
        </abbr><br><br> 
          
        <button onclick = "myGeeks()"> 
            Submit 
        </button> 
          
        <p id = "geeks"></p> 
    </body> 
</html>                    

輸出:
在單擊按鈕之前:

單擊按鈕後:

範例2:可以使用document.createElement方法創建縮寫對象。

<!DOCTYPE html> 
<html> 
    <head> 
        <title> 
            HTML DOM Abbreviation Object 
        </title> 
          
        <!-- script for Abbreviation Object -->
        <script> 
            function myGeeks() { 
                var w = document.createElement("ABBR"); 
                var f = document.createTextNode("GFG"); 
                w.setAttribute("title", "GeeksforGeeks"); 
                w.appendChild(f); 
                document.getElementById("sudo").appendChild(w); 
            } 
        </script> 
    </head> 
      
    <body> 
        <h2>DOM Abbreviation Object</h2> 
          
        <button onclick = "myGeeks()"> 
            Submit 
        </button> 
  
        <p id = "sudo"></p> 
          
    </body> 
</html>                    

輸出:
之前單擊按鈕:

單擊按鈕後:

支持的瀏覽器:下麵列出了DOM縮寫對象支持的瀏覽器:

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




相關用法


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