Can anyone explain why focus() is not working alwa

2019-07-04 17:00发布

问题:

I have the following code, which works 100% ok on Chrome and Safari, but on IE 10 sometimes works and sometimes don't.

Sys.Focus = function(obj){    
    if(Sys.Anim.length>0){
       Sys.Fp = obj;
       return;
    }
    obj.focus();
}

.
.
.

function Animate(...){
   var i,...
   .
   .
   .

   if(Finished(Sys.Anim[i])){
      Sys.Anim.splice(i,1);
      if(Sys.Anim.length==0){
         if(Sys.Fp){
            Sys.Focus(Sys.Fp)
            Sys.Fp = null;
         }
      }
   }
   .
   .
   .    
}

.
.
.

email = document.getElementById("email");
Sys.Focus(email);
email.onkeydown = function(){
   debugger
   .
   .
   .
}

In response to different user actions, some objects on screen either change color or move around, this is done by Animate(), objects to be animated are added to an Array (Sys.Anim) and removed when the animation ends. In order to keep everything smooth, if the page becomes ready for input before the animation ends (which almost always happens), the focus() call is delayed until the animation ends, that is about 1/3 of a second.

Everything works just as expected in all browsers except IE 10. At first I thought there was a logic error on my code, however I debugged it with the Developer Tools and I discovered all the steps are carried on correctly, the problem is that focus() is not actually working all the times.

Another important detail... when focus() succeeds email.onkeydown is executed every time I hit a key, however when focus() fails I obviously must click on the input control to focus it manually, but when this happens the email.onkeydown function is never called even when the content of the input control is updated with every key punch.

I tryed:

setTimeout(function(){obj.focus()},100);

which was proposed as a solution for this problem, but it doesn't solve mine.

Why this happens and how can be worked around?

UPDATE:

For testing proposes I added the following function:

email.onfocus = function(){
   debugger
}

which brings the debugger only when focus() succeeds, if focus() fails the debugger won't pop up even if you focus the input control manually with a mouse click, because in this case I simply cannot focus the input control by using the Tab or Shift-Tab keys... is just as if it didn't exist!!!

回答1:

Solved!!!

After lots of frustrating tests I discovered that the input control was nested inside a DIV which in some circumstances was disabled, dumb of me to disable a DIV.

...However all other browsers only actually disable the input control if it is explicitly disabled. The guys at Microsoft always trying to be "too clever" decided to take a conterintuitive approach and leave it half done.

The problem and my complaint is that the input control does not look disabled, it looks normal and the caret actually appears if you click on it, you can even type on it no matter how disabled it was supposed to be... so for the record, always remember IE 10 only half disables input controls which are inside disabled DIV giving you no visual clue of what's going on.