焦点在IE中不起作用

Sim*_*mon 15 html javascript css jquery

我有以下功能

 function change() 
 {
       var input = document.getElementById('pas');
       var input2 = input.cloneNode(false);
       input2.type = 'password';
       input.parentNode.replaceChild(input2,input);
       input2.focus();
  }
Run Code Online (Sandbox Code Playgroud)

但是focus() 在ie7中不起作用,所以我该怎么办!我希望将光标放在输入内!

谢谢

更新

伟大的解决方案,谢谢,但现在它在歌剧中不起作用:(

zaf*_*zaf 36

对于IE,你需要使用settimeout函数,因为它是懒惰的,例如:

setTimeout(function() { document.getElementById('myInput').focus(); }, 10);
Run Code Online (Sandbox Code Playgroud)

来自http://www.mkyong.com/javascript/focus-is-not-working-in-ie-solution/

对于opera来说,这可能有所帮助: 如何在Opera的文本框上设置所需索引的焦点

  • 我喜欢直截了当'由于它是懒惰的'解释:) (3认同)