I am trying to achieve two things with a single onclick of anchor tag. First, when I click on search, I am scrolling to a particular di开发者_如何学JAVAv, and then trying to focus on the textbox within that div. Below is the link to the sample code. I am able to scroll to the div, but somehow the textbox is losing focus. What is wrong with the code?
http://jsfiddle.net/e9tJ5/1/
You need to prevetDefault.
$("#searchlink").click(function(e){
e.preventDefault();
$("#searchtext").focus();
});
Return false also does the same.
Check working example at http://jsfiddle.net/e9tJ5/5/
Works fine in all browsers tested except Firefox. See here for more info: http://www.symphonious.net/2010/11/24/controlling-focus-in-firefox/
I've added second $("#searchtext").focus();
and it works in firefox. not elegant but works.
$("#searchlink").click(function(){
$("#searchtext").focus();
return false;
});
精彩评论