最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Text Input focus anomaly in IE9 - Stack Overflow

programmeradmin1浏览0评论

I am trying out this rather simple code. It is supposed to shift the focus to the neighboring text input whenever there is mouseup on the first one:

<!DOCTYPE html>
<html>  
  <head>
    <script src="//ajax.googleapis/ajax/libs/jquery/1.8.3/jquery.min.js">   
    </script>
    <script>
      $(document).ready(function() {
        $(".foo").mouseup(function() {
          $(".boo").focus();
        });
      });   
    </script>
  </head> 
  <body>
    <input type='text' class='foo' />
    <input type='text' class='boo' />
  </body>    
</html>

In all browsers except IE9 (I didn't dare test it in IE8 or below!), it works fine for both left button and right button (for right button mouseup, the context menu still pops up on the first input even though the focus shifts to the 2nd one). However, in IE9, the focus still remains on the first text input (the context menu also popping up there).

Is there any way to fix it so that the focus shifts to the 2nd one?

I am trying out this rather simple code. It is supposed to shift the focus to the neighboring text input whenever there is mouseup on the first one:

<!DOCTYPE html>
<html>  
  <head>
    <script src="//ajax.googleapis./ajax/libs/jquery/1.8.3/jquery.min.js">   
    </script>
    <script>
      $(document).ready(function() {
        $(".foo").mouseup(function() {
          $(".boo").focus();
        });
      });   
    </script>
  </head> 
  <body>
    <input type='text' class='foo' />
    <input type='text' class='boo' />
  </body>    
</html>

In all browsers except IE9 (I didn't dare test it in IE8 or below!), it works fine for both left button and right button (for right button mouseup, the context menu still pops up on the first input even though the focus shifts to the 2nd one). However, in IE9, the focus still remains on the first text input (the context menu also popping up there).

Is there any way to fix it so that the focus shifts to the 2nd one?

Share Improve this question edited Jul 1, 2016 at 16:03 NedStarkOfWinterfell asked Feb 24, 2013 at 22:16 NedStarkOfWinterfellNedStarkOfWinterfell 5,24328 gold badges112 silver badges204 bronze badges 3
  • Can you try doing this in pure JS just to see if it works? So document.getElementById(element).focus(); – karancan Commented Feb 24, 2013 at 22:28
  • Lemme try. Give me two minutes... – NedStarkOfWinterfell Commented Feb 24, 2013 at 22:29
  • Nope. It doesn't work, as expected..:( – NedStarkOfWinterfell Commented Feb 24, 2013 at 22:30
Add a ment  | 

2 Answers 2

Reset to default 3

.select() in ie should work:

http://jsfiddle/n4hGC/5/

$(document).ready(function(){
  $('.foo').mouseup(function() {
    $('.boo').focus();
  });
  $('.foo').contextmenu(function() {
      $('.boo').select();
      return false;
  });
});

Have also handled the context menu by returning false. Hope helps!

Can you try one of these alternatives? They have worked for me in IE8 in the past and they might work for you.

Alternative 1: Blur then Focus

$(".foo").mouseup(function(){
    $(".boo").blur();  
    $(".boo").focus();
});

Alternative 2: Using setTimeout

$(".foo").mouseup(function(){ 
    setTimeout(function () {
        $(".boo").focus();
    }, 100);  
});
发布评论

评论列表(0)

  1. 暂无评论