• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Tony Qu
我的软件工作室
博客园    首页    新随笔    联系   管理     

how to focus to a div both in firefox and IE

The following code work well in IE to focus to a div:

document.getElementById("div1").focus();

However, it doesn't work in firefox. You will not see any action taken after the line is executed. To workaround the issue, you can use the following code:


function getOffsetTop(el)
{
    
var yPos = el.offsetTop;
    
var tempEl = el.offsetParent;
    
while (tempEl != null)
    
{
        yPos 
+= tempEl.offsetTop;
        tempEl 
= tempEl.offsetParent;
    }

    
return yPos; 
}


window.scrollTo(
0,getOffsetTop(e)); 

getOffsetTop method is used to get the accurate top pixel of any element.

Here, window.scrollTo has two parameters. The first one represents offsetX and the second one represents offsetY. Here we only use the second parameter to scroll to the top border of the target control.


版权声明:本文由作者Tony Qu原创, 未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。
posted @ 2007-07-24 12:38  找事的狐狸  阅读(1109)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3