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


HTML Anchor hostname用法及代碼示例


HTML DOM中的DOM錨主機名屬性用於設置或返回href屬性值的主機名部分。

用法:

  • 它返回主機名屬性。
    anchorObject.hostname
  • 它用於設置主機名屬性:
    anchorObject.hostname = hostname

屬性值:它包含值,即主機名,它指定URL的主機名。


返回值:它返回代表URL的域名或IP地址的字符串值。

示例1:本示例返回主機名屬性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML DOM Anchor hostname Property 
    </title> 
</head> 
  
<body> 
    <center> 
        <h1> 
          GeeksForGeeks 
      </h1> 
  
        <h2> 
          DOM Anchor hostname Property 
      </h2> 
  
        <p>Welcome to 
            <a href= 
      "http://www.example.com:4097/test.htm#part2" 
               id="GFG" 
               rel="nofollow"
               hreflang="en-us" 
               target="_self">  
                GeeksforGeeks  
            </a> 
        </p> 
  
        <button onclick="myGeeks()"> 
          Submit 
      </button> 
  
        <p id="sudo" 
           style="color:green; 
                  font-size:25px;"> 
      </p> 
  
        <script> 
            function myGeeks() { 
                
                var x =  
                    document.getElementById("GFG").hostname 
                  
                document.getElementById( 
                  "sudo").innerHTML = x; 
            } 
        </script> " 
    </center> 
</body> 
  
</html>

輸出:

在單擊按鈕之前:

單擊按鈕後:

示例2:本示例設置主機名屬性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML DOM Anchor hostname Property 
    </title> 
</head> 
  
<body> 
    <center> 
        <h1> 
          GeeksForGeeks 
      </h1> 
  
        <h2> 
          DOM Anchor hustname Property 
      </h2> 
  
        <p>Welcome to 
            <a href= 
       "http://www.example.com:4097/test.htm#part2" 
               id="GFG"
               rel="nofollow"
               hreflang="en-us"
               target="_self">  
                GeeksforGeeks  
            </a> 
        </p> 
  
        <button onclick="myGeeks()"> 
          Submit 
      </button> 
  
        <p id="sudo"
           style="color:green; 
                  font-size:25px;"> 
      </p> 
  
        <script> 
            function myGeeks() { 
                var x =  
                    document.getElementById( 
                      "GFG").hostname =  
                    "www.GeeksForGeeks.org" 
                  
                document.getElementById( 
                  "sudo").innerHTML = x; 
            } 
        </script> " 
    </center> 
</body> 
  
</html>

輸出:

在單擊按鈕之前:

單擊按鈕後:

支持的瀏覽器:下麵列出了DOM Anchor主機名屬性支持的瀏覽器:

  • 穀歌瀏覽器
  • Internet Explorer 10.0以上
  • Firefox
  • Opera
  • Safari


相關用法


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