在RHEL5中,Firefox无法将焦点转移到另一个文本输入

Can't shift focus to another text input in Firefox in RHEL5

本文关键字:转移 焦点 另一个 输入 文本 RHEL5 Firefox      更新时间:2023-09-26

我正在尝试这个简单的代码:

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

它应该做的是在第一个文本输入上右键单击时将焦点转移到第二个文本输入。这在Windows上的浏览器中工作得很好,但在RHEL5中,当检查Firefox时,这不会发生,并且焦点仍然留在第一个。

我试着把逻辑改成这样:

$(".foo").click(function(e) {
   if (e.which == 3)
     $(".boo").focus();
});

还是不行。但是,如果我将最后一个代码更改为if (e.which == 1)...,则焦点会在单击时转移,而不是右键单击。这个问题发生在RHEL5的Firefox, Firefox, Chrome在Mac.然而,它在Mac上的Safari和Opera工作良好。

我该如何解决这个问题?

首先将输入类更改为id,这里更合适

<input type="text" id="foo" /><input type="text" id="boo" />

那么试试这个,简单的javascript,不需要jQuery。

document.getElementById("foo").onfocus = function(){
  document.getElementById("boo").focus();
}

编辑:在OP的原始代码中使用超时的工作解决方案:

setTimeout(function(){$(".boo").focus()}, 0)