问题
I have a messaging function in my site that when a user clicks on a reply button they get redirected to the message and have the textarea focus based from the anchor link.
so the link structure is like this:
view_message/4fad37da1df#reply
thanks
回答1:
Try:
if (window.location.hash) {
$(window.location.hash).attr("tabindex", -1).focus();
}
It should check if the url has a hash in it, and if it does, give the target the tabindex attribute with a value of -1 and apply focus.
See https://stackoverflow.com/a/6188217/430191 for the reason for the tabindex attribute (a related issue about keyboard focus in IE/Chrome/Safari)
来源:https://stackoverflow.com/questions/10593111/focus-on-textarea-based-from-anchor-link