解决chrome在onblur事件中同时使用alert和focus,循环弹框

转自:

https://stackoverflow.com/questions/48536618/onblur-onfocus-infinite-loop-issue-in-chrome

Enter your number: 

1.Setting the field value to empty value.(在数值可以清空时)

function myFunction(field)
{
    if(isNaN(field.value))
    {
        alert('wrong !!!');
        field.value="";
        field.focus();
        return false;
    }
}

2.In case, you don't need to clear the value, focus the element in a setTimeout.(切换浏览器标签仍会出现循环弹框)

function myFunction(field)
{
    if(isNaN(field.value))
    {
        alert('wrong !!!');
        setTimeout(function(){
            field.focus();
        },0);
        return false;
    }
}

3.Removing and adding the onblur event after alert finishes.(可以解决)

function myFunction(field)
{
    if(isNaN(field.value))
    {
        alert('wrong !!!');
        var onblurevent=field.onblur;
        field.onblur = "";
        setTimeout(function(){
            field.focus();
            field.onblur=onblurevent;
        },0);
        return false;
    }
}

 

你可能感兴趣的:(解决chrome在onblur事件中同时使用alert和focus,循环弹框)