Home  >  Article  >  Web Front-end  >  The javascript code that makes the first control on the page selected by default_form effects

The javascript code that makes the first control on the page selected by default_form effects

WBOY
WBOYOriginal
2016-05-16 18:38:061057browse

When a form is displayed on the page, the focus is usually not on the first control. This effect can be achieved with just a little common script.
document.forms[0].elements[0].focus();
If the first control on the page is a hidden field, or does not support focus(), the above javascript may cause an error!

Copy code The code is as follows:

var formutil = new Object;
formutil.focusOnfirst = function()
{
if(document.forms.length>0){
var item = document.forms[0].elements.length;
for(var i=0;i< ;item;i ){
if(document.forms[0].elements[i].type !="hidden"){
document.forms[0].elements[i].focus();
return;
}
}
}
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn