HTML DOM输入URL maxLength属性

HTML DOM Input URL maxLength Property

HTML DOM输入URL的maxLength属性返回/设置输入URL的maxLength属性。 如果未定义,则此属性返回" -1"。

句法

以下是语法-

  • 返回maxLength属性
1
inputURLObject.maxLength

  • 将maxLength属性设置为一个数字
1
inputURLObject.maxLength = number

让我们看一下输入网址maxLength属性的示例-

现场演示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html>
<html>
<head>
Input URL maxLength
<style>
 form {
   width:70%;
   margin: 0 auto;
   text-align: center;
 }
 * {
   padding: 2px;
   margin:5px;
 }
 input[type="button"] {
   border-radius: 10px;
 }
</style>
</head>
<body>
<form id="Google">
<fieldset>
<legend>URL-maxLength</legend>
<label for="URLSelect">URL :
<input type="url" id="URLSelect" maxLength="10">
</label>
<input type="button" onclick="logIN()" value="Log In">
<input type="button" onclick="changeMaxLength()" value="Change Maxlength">

</fieldset>
</form>

 var divDisplay = document.getElementById("divDisplay");
 var inputURL = document.getElementById("URLSelect");
 divDisplay.textContent = 'Maxlength: '+inputURL.maxLength;
 function changeMaxLength() {
   inputURL.maxLength += 10;
   divDisplay.textContent = 'Maxlength: '+inputURL.maxLength;
 }
 function logIN(){
   if(inputURL.value !== '')
    divDisplay.textContent = 'Could not identify url, Redirecting to '+inputURL.form.id;
 }

</body>
</html>

输出量

这将产生以下输出-

在点击"更改最大长度"按钮之前-

点击"更改最大长度"按钮后-

点击"登录"按钮并设置电子邮件输入后-