onFocus无法在Chrome、Firefox、Safari中工作

onFocus not working in Chrome, Firefox, Safari

本文关键字:Firefox Safari 工作 Chrome onFocus      更新时间:2023-09-26

我有个问题。。。我在onFocus事件下调用一个函数,而该函数在onFocusevent下不能正常工作,但当我将该函数放在onMouseOver下时,它工作得很好。。。。为什么不在onFocus事件下运行函数??

html代码如下::

<a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image4','','./Images/Search1.gif',1);" onfocus="commonSearchValidation('company','companyname','branchcode','isactive'); chkSpecial('company'); chkSpecial('branchcode');" onclick="clickSearch('company','branchcode','companyNameOpr','companyname','isactive');" >
                <img src="Images/Search.gif"  alt="Search"     name="Image4" width="73" height="21" border="0" id="Image4" /></a> 

我调用的函数如下:

function commonSearchValidation()
{   
    var counter=arguments.length;   
    var resflag = false;        
    for(var i=0 ; i < counter ; i++)
    {
        var fvalue = document.getElementById(arguments[i]).value;
        if(fvalue.trim().length!=0)
        {           
            resflag = true;
        }                   
    }
    if(resflag)
    {   
        return resflag;
    }
    else
    {
        alert(Enteronefield);
        document.getElementById(arguments[0]).focus();
        return resflag;
    }
}

因为您误解了聚焦意味着什么。它与onmouse-over不同。

<a href="#" onfocus="myFunction(this)">onfocus</a>
<br>
<br>
<a href="#" onmouseover="myFunction(this)">onmouseover</a>

和js代码

 function myFunction(x)
    {
    alert("This is "+x.text);
    document.getElementById("a2").focus();   
    }

这是工作[JSfiddle DEMO][1]。了解区别。