更新时间:2023-02-25 17:37:50
我明白了.目标应该是一个可以聚焦的标签,比如链接,如果不是,我的情况是一个div,应该将目标的tabindex设置为-1.
I get it. The target should be a tag that can be focused, like a link, if not, which is my case a div, should set tabindex of the target as -1.
我的 jQuery 解决方案,带有 ScrollTo 插件,是:
My jQuery solution, with ScrollTo plug-in, is:
$("a[href^='#']")
.click(function(evt){
var j = $(evt.currentTarget);
var anchorTarget = j.attr("href");
$("body")
.scrollTo(anchorTarget, 500, {
onAfter:function() {
window.location.hash = anchorTarget.substr(1);
$(anchorTarget).attr("tabindex",-1).focus();
}
});
evt.preventDefault();
});