代码如下,采用:window.onload=FormUtil.focusOnFirst();调用对象方法,没有什么效果,但如果是:<body onload="FormUtil.focusOnFirst();" >
能够实现正常的效果。这是什么原因造成的?var FormUtil=new Object;
FormUtil.focusOnFirst=function()
{
if(document.forms.length>0)
{
for(var i=0; i<document.forms[0].elements.length; i++)
{
var oField=document.forms[0].elements[i];
if(oField.type!="hidden")
{
oField.focus();
return;
}
}
}
}
window.onload=FormUtil.focusOnFirst();
能够实现正常的效果。这是什么原因造成的?var FormUtil=new Object;
FormUtil.focusOnFirst=function()
{
if(document.forms.length>0)
{
for(var i=0; i<document.forms[0].elements.length; i++)
{
var oField=document.forms[0].elements[i];
if(oField.type!="hidden")
{
oField.focus();
return;
}
}
}
}
window.onload=FormUtil.focusOnFirst();
谢谢,请教一下,为什么有时候需要这样写呢?并且也能实现一些正常的效果。window.onload=function(){ ...... }
不管是匿名函数 有名的函数 还是变量引用的函数//可以算是一个变量引用的函数
window.onload=FormUtil.focusOnFirst//匿名函数
window.onload=function()
{
if(document.forms.length>0)
{
for(var i=0; i<document.forms[0].elements.length; i++)
{
var oField=document.forms[0].elements[i];
if(oField.type!="hidden")
{
oField.focus();
return;
}
}
}
}//有名的函数
function focusOnFirst(){
if(document.forms.length>0)
{
for(var i=0; i<document.forms[0].elements.length; i++)
{
var oField=document.forms[0].elements[i];
if(oField.type!="hidden")
{
oField.focus();
return;
}
}
}
}
window.onload = focusOnFirst;