2011-05-05 4 views
3

I가 포커스 이벤트가 나는 다음과 같은 출력을 얻을 IE8에서 아래의 코드에서 처리에 문제 :IE 포커스 이벤트 핸들러는 지연

LOG: set focus txt1 
LOG: set focus txt2 
LOG: txt1 focus 
LOG: txt2 focus 

다른 모든 브라우저에서 출력은 다음과 같습니다

LOG: set focus txt1 
LOG: txt1 focus 
LOG: set focus txt2 
LOG: txt2 focus 

을 IE8은 모든 포커스 요청을 대기열에 넣고 현재 함수가 끝난 후 이벤트 핸들러를 실행하는 것 같습니다.

IE8이 다른 브라우저처럼 잘 작동하도록하는 해결 방법이 있습니까?

<html> 
<head> 
</head> 
<body> 
<script> 
     function test(){ 
      console.log('set focus txt1'); 
      document.getElementById('txt1').focus(); 
      console.log('set focus txt2'); 
      document.getElementById('txt2').focus(); 
     } 
    </script> 
<input id="txt1" type="text" onfocus="javascript:console.log('txt1 focus')" style="width:100px" /> 
<input id="txt2" type="text" onfocus="javascript:console.log('txt2 focus')" style="width:100px" /> 
<button value="Click" onclick="javascript:test()"></button> 
</body> 
</html> 

답변

3

IE는 함수 test()이 완료 될 때까지 실제 초점을 지연, 그래서 당신이 같은 건설 사용할 필요가 두려워 : 그 때문에 함수의 두 번째 부분을 연기 여기

function test(){ 
    console.log('set focus txt1'); 
    document.getElementById('txt1').focus(); 

    window.setTimeout(function() { 
     console.log('set focus txt2'); 
     document.getElementById('txt2').focus(); 
    }, 1); 
} 

을 IE는 두 번째 부분이 실행되기 전에 txt1에 포커스를 설정할 시간이 있습니다.

그런데 onclick 및 onfocus 속성에서 접두어 javascript:을 생략해야합니다. javascript: 접두사는 href 특성에서만 사용해야합니다.

관련 문제