2

My problem occurs in IE10 and Firefox, but works in Chrome.

I have an anchor like this:

<a id="link-to-form" href="#bonus-form">

An also I have an input element that I have it focused using jquery:

<input type="text" id="firstname" name="firstname" required />

$(document).ready(function(){                   
   $('#firstname').focus();     
});

My problem is that when I press the anchor link, and it moves inside the page, the focus is lost from my input tag. I would like to keep the focus on my input, after I press the anchor link.

I have tried many things, last one was the following. But still it doesn't focus. Actually the click event is fired on the debugger, but immediately the anchor executes and it loses focus again:

$("#link-to-form").on("click", function (e) {
    $('#firstname').focus();
    return true;            
});
4

1 回答 1

2

诀窍可能是存储当前聚焦的元素,
regainLastFocus类添加到任何所需的锚点
并单击该类元素:将焦点重置为上次聚焦的存储的 jQuery 元素

不要忘记event点击并使用:

event.preventDefault();

$("#link-to-form").on("click", function (e) {
    e.preventDefault();
    $('#firstname').focus();         
});
于 2013-06-25T14:42:20.730 回答