将焦点设置在屏幕jquery的顶部 [英] set Focus on Top of screen jquery

查看:89
本文介绍了将焦点设置在屏幕jquery的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个标签的页面,我根据使用jquery的href点击显示和隐藏它们。每当我点击a href时,页面就会向下滚动。我试图将焦点设置在页面顶部。这是我在Jquery的第一个项目。我尝试了以下但仍然无法正常工作。

I have a page with multiple tabs and i Show and hide them based on a href click using jquery. Whenever I click on "a href", the page is scrolled down. Am trying to set the focus on the top of the page. This is my first project in Jquery. I tried the following but still doesnt work.


  1. scroll(0,0);

  2. window.scrollTo(0,0);

  3. window.scrollTo(0,document.body.scrollHeight);

  4. var ctrl = document.getElementById('lnkCreateJob');
    ctrl.focus();

  1. scroll(0, 0);
  2. window.scrollTo(0,0);
  3. window.scrollTo(0, document.body.scrollHeight);
  4. var ctrl = document.getElementById('lnkCreateJob'); ctrl.focus();

请分享一下这个问题。

推荐答案

试试这样:

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

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

这篇关于将焦点设置在屏幕jquery的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆