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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。