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:
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));
{
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原创, 未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。