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


HTML Input URL defaultValue用法及代碼示例


HTML DOM中的DOM輸入URL defaultValue屬性用於設置或返回URL字段的默認值。此屬性用於反映HTML值屬性。默認值和值之間的主要區別在於,默認值表示默認值,並且在進行一些更改後該值包含當前值。此屬性對於確定URL字段是否已更改很有用。

用法:

  • 它返回defaultValue屬性。
    urlbject.defaultValue
  • 它用於設置defaultValue屬性。
    urlObject.defaultValue = value

屬性值:它包含一個屬性值,該屬性值定義URL字段的默認值。


返回值:它返回一個表示URL字段默認值的字符串值。

示例1:本示例說明了如何返回Input URL defaultValue屬性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        DOM Input URL defaultValue Property 
    </title> 
</head> 
  
<body> 
    <center> 
        <h1 style="color:green;">  
                GeeksForGeeks  
            </h1> 
  
        <h2> 
          DOM Input URL defaultValue Property 
      </h2> 
  
        <label for="uname"
               style="color:green"> 
      </label> 
  
            <form id="geeks"> 
                <input type="url"
                       id="gfg"
                       placeholder="Enter URL" 
                       size="20" 
                       value="GeeksForGeeks" 
                       pattern="https?://.+"
                       title="Include http://"
                       maxlength="20"> 
            </form> 
            <br> 
            <br> 
  
            <button type="button"
                    onclick="geeks()"> 
                Click 
            </button> 
  
            <p id="GFG" 
               style="color:green;font-size:25px;"> 
          </p> 
  
            <script> 
                function geeks() { 
                    
                    var link =  
                        document.getElementById( 
                          "gfg").defaultValue; 
                    
                    document.getElementById( 
                      "GFG").innerHTML = link; 
                } 
            </script> 
    </center> 
</body> 
  
</html>

輸出:
在單擊按鈕之前:

單擊按鈕後:

示例2:本示例說明如何設置屬性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        DOM Input URL defaultValue Property 
    </title> 
</head> 
  
<body> 
    <center> 
        <h1 style="color:green;">  
                GeeksForGeeks  
            </h1> 
  
        <h2> 
          DOM Input URL defaultValue Property 
      </h2> 
  
        <label for="uname"
               style="color:green"> 
      </label> 
  
            <form id="geeks"> 
                <input type="url"
                       id="gfg" 
                       placeholder="Enter URL"
                       size="20" 
                       value="GeeksForGeeks" 
                       pattern="https?://.+"
                       title="Include http://"
                       maxlength="20"> 
            </form> 
            <br> 
            <br> 
  
            <button type="button"
                    onclick="geeks()"> 
                Click 
            </button> 
  
            <p id="GFG"
               style="color:green; 
                      font-size:25px;"> 
          </p> 
  
            <script> 
                function geeks() { 
                    
                  var link =  
                    document.getElementById( 
                     "gfg").defaultValue = "finecomb"; 
                    
                    document.getElementById( 
                      "GFG").innerHTML =  
                      "The defaultvalue was changed to " 
                    + link; 
                } 
            </script> 
    </center> 
</body> 
  
</html>

輸出:
在單擊按鈕之前:

單擊按鈕後:

支持的瀏覽器:下麵列出了DOM輸入URL defaultValue屬性支持的瀏覽器:

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


相關用法


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