Javascript 焦点函数在这里有什么用途?有人能解释一下下面的代码吗?i';我是初学者,所以我需要知道焦点函数的用法
需要知道焦点函数在这里的用法。 有人能解释一下这个代码吗Javascript 焦点函数在这里有什么用途?有人能解释一下下面的代码吗?i';我是初学者,所以我需要知道焦点函数的用法,javascript,Javascript,需要知道焦点函数在这里的用法。 有人能解释一下这个代码吗 function Allow Age(){ if(!form.age.value.match(/[0-9]+$/) && form.age.value !="") { form.age.value=""; form.age.focus(); alert("invalid format"); } if(form.age.value.length > 1)
function Allow Age(){
if(!form.age.value.match(/[0-9]+$/) && form.age.value !="")
{
form.age.value="";
form.age.focus();
alert("invalid format");
}
if(form.age.value.length > 1)
alert("invalid entry")
}
.focus()
使文本框中显示闪烁的行,以便您可以开始键入。以后先用谷歌搜索一下。这意味着表单中的age
字段将被删除。focus()
将导致光标显示在该字段中,因此当用户键入时,他们会键入该字段。这与用户在输入字段之前单击字段时相同。此代码中的第一条If语句是验证用户输入的年龄是否为数字,而不是空值。第二个用于检查输入年龄的长度是否大于1(不确定为什么会出现此情况,但您的语句就是这样做的)。
对于第一个问题,焦点用于为文本框提供闪烁的光标,即:将其设置为准备接收用户输入。
在伪代码中,它是:
function Allow Age(){
If age is not numeric and its value is not null
Clear the textbox
Set focus to the textbox
Send a message saying "Invalid format"
End if
If the length of the age entered is greater than 1
Send a message saying "Invalid Entry"
End if
}
element.focus()
执行名称中的操作-将键盘光标聚焦在元素上。