gpt4 book ai didi

jquery - 动画后如何设置焦点

转载 作者:行者123 更新时间:2023-12-01 02:03:11 24 4
gpt4 key购买 nike

我想在使用 div.focus 和 animate() 滚动到页面顶部后将焦点设置在 id 的文本框(假设为 txtName)上。

是否有一个功能指定在动画之后聚焦,就像 document.ready 在 document.ready 之后执行一样?

下面给出了我用来滚动的代码。

$(".gototop").click(function(){ 
var focusElement = $("#contents");
$(focusElement).focus();
ScrollToTop(focusElement);
});

function ScrollToTop(el) {
$('html, body').animate({ scrollTop: $(el).offset().top - 50 }, 'slow');
}

最佳答案

您可以在 animate 回调中将焦点设置到某个元素,这样当动画完成时它将获得焦点:

function ScrollToTop(el) { 
$('html, body').animate({
scrollTop: $(el).offset().top - 50 },
'slow', function() {
$("#txtName").focus();
});
}

如果 el 是您希望聚焦的元素,则可以使用传递的变量而不是 #txtName

关于jquery - 动画后如何设置焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12210572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com