jquery settimeout和.focus()一起在firefox浏览器中不起作用

我有一个表单,当提交时(空字段)显示前端jsp中的弹簧validation错误。 我需要将显示屏集中在前端显示的第一条错误消息中。 从表单生成的代码(通过视图源):例如,div元素中包含的2错误消息。 我需要把重点放在第一条错误消息上。

User id is missing
age is missing

我尝试了下面的jquery代码,但它只适用于IE而不适用于Firefox。 在Firefox中,焦点不起作用,尽管css样式处理得当。

我确信语法和逻辑是正确的 – 有人可以帮我修复firefox浏览器吗?

    $(document).ready(function() { $("[id$='.errors']:first").css("background-color","#FFFFCC"); setTimeout(function(){ $("[id$='.errors']:first").focus(); },300); });  

我不明白你为什么要集中一个div和你的期望,通常你不能集中div,但你可以这样做,

 $(document).ready(function() { $("[id$='.errors']:first").css("background-color","#FFFFCC"); setTimeout(function(){ $("[id$='.errors']:first").attr("tabindex",-1).focus(); alert(document.activeElement.innerHTML+ "is focussed"); },300); });​​ 

http://jsfiddle.net/x7euD/3/