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


HTML Anchor port用法及代碼示例


HTML DOM中的DOM錨端口屬性用於設置或返回href屬性值的端口部分。它的默認值為80或443。如果未在URL中指定端口號,則某些瀏覽器將返回0或未定義。

用法:

  • 它返回端口屬性。
    anchorObject.port
  • 它用於設置端口屬性。
    anchorObject.port = number

屬性值:它包含值,即數字,它定義了URL的端口號。


返回值:它返回一個表示URL端口號的字符串值。

示例1:本示例返回URL的端口號。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML DOM Anchor port Property 
    </title> 
</head> 
  
<body> 
    <center> 
        <h1> 
          GeeksForGeeks 
      </h1> 
  
        <h2> 
          DOM Anchor port Property 
      </h2> 
  
        <p> 
          Welcome to <a href= 
          "http://example.com:12345/gfg');" 
                        id="GFG" 
                        rel="nofollow"
                        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").port; 
                
                document.getElementById( 
                  "sudo").innerHTML = x; 
            } 
        </script>  
    </center> 
</body> 
  
</html>

輸出:
在單擊按鈕之前:

單擊按鈕後:

示例2:本示例設置或更改URL的端口號。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML DOM Anchor port Property 
    </title> 
</head> 
  
<body> 
    <center> 
        <h1> 
          GeeksForGeeks 
      </h1> 
  
        <h2> 
          DOM Anchor port Property 
      </h2> 
  
        <p>Welcome to <a href= 
                         "http://example.com:12345/gfg');"
                         id="GFG" 
                         rel="nofollow" 
                         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").port = "443"; 
                
                document.getElementById( 
                  "sudo").innerHTML =  
                  "The port number was changed to " + x; 
            } 
        </script> " 
    </center> 
</body> 
  
</html>

輸出:

在單擊按鈕之前:

單擊按鈕後:

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

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


相關用法


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