jQuery focus function not working in Firefox

2019-01-29 02:44发布

The following piece of code focuses the text input after you click on the link. It works fine for Chrome 2.x, IE8 and Opera 9.64 but not on Firefox 3.0.9. The text input flashes quickly in Firefox then disappears, I'm currently working on Windows XP SP2.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> 
<script>
$(document).ready(function()
{
    $("a").click(function() {
        var field_id = $(this).attr("href");
        $(field_id).focus();
    });
});
</script>

<a href="#text_field">Focus</a>
<input type="text" name="text_field" id="text_field" />

Does anyone know how to handle the above in Firefox?

7条回答
手持菜刀,她持情操
2楼-- · 2019-01-29 03:15

You could also be more explicit and call preventDefault on the event arg.

$(document).ready(function()
{
    $("a").click(function(event) {
        var field_id = $(this).attr("href");
        $(field_id).focus()
        event.preventDefault();
    });

});
查看更多
登录 后发表回答