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


HTML Input URL maxLength用法及代码示例


HTML DOM中的DOM输入URL maxLength属性用于设置或返回URL输入字段的maxlength属性值。它指定URL字段中允许的最大字符数。输入搜索maxLength属性的默认值为524288。

用法:

  • 它返回Input url maxLength属性。
    urlObject.maxLength
  • 它用于设置Input url maxLength属性。
    urlObject.maxLength = number
  • 属性值:它包含一个单一的数字,用于指定搜索maxlength字段中允许的最大字符数。


    返回值:它返回一个数字值,该值表示URL maxlength字段中允许的最大字符数。

    示例1:本示例说明了如何返回Input URL maxLength属性。

    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <title> 
            DOM Input URL maxLength Property 
        </title> 
    </head> 
      
    <body> 
        <center> 
            <h1 style="color:green;">  
                    GeeksForGeeks  
                </h1> 
      
            <h2> 
              DOM Input URL maxLength Property 
          </h2> 
      
            <label for="uname" 
                   style="color:green"> 
                <b>Enter URL</b> 
          </label> 
      
                <input type="url"
                       id="gfg"
                       placeholder="Enter URL"
                       size="20"
                       pattern="https?://.+" 
                       title="Include http://" 
                       maxlength="20"> 
      
                <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").maxLength; 
                        
                        document.getElementById( 
                          "GFG").innerHTML = link; 
                    } 
                </script> 
        </center> 
    </body> 
      
    </html>

    输出:
    在单击按钮之前:

    单击按钮后:

    示例2:本示例说明了如何设置属性。

    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <title> 
            DOM Input URL maxLength Property 
        </title> 
    </head> 
      
    <body> 
        <center> 
            <h1 style="color:green;">  
                    GeeksForGeeks  
                </h1> 
      
            <h2> 
              DOM Input URL maxLength Property 
          </h2> 
      
            <label for="uname" 
                   style="color:green"> 
                <b>Enter URL</b> 
          </label> 
      
                <input type="url"
                       id="gfg" 
                       placeholder="Enter URL" 
                       size="20" 
                       pattern="https?://.+" 
                       title="Include http://"
                       maxlength="20"> 
      
                <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").maxLength = "40"; 
                          
                      document.getElementById( 
                        "GFG").innerHTML =  
                        "The value of the maxLength"+ 
                        " attribute was changed to " + link; 
                    } 
                </script> 
        </center> 
    </body> 
      
    </html>

    输出:
    在单击按钮之前:

    单击按钮后:

    支持的浏览器:DOM输入URL maxLength属性支持的浏览器如下:

    • 谷歌浏览器
    • Internet Explorer 10.0以上
    • Firefox
    • Opera
    • Safari


相关用法


注:本文由纯净天空筛选整理自ManasChhabra2大神的英文原创作品 HTML | DOM Input URL maxLength Property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。