开发者

issue with getting position of an element

开发者 https://www.devze.com 2023-01-23 01:37 出处:网络
im having issue with getting position of an element. there are 4divs with position relative. but and float is left. when im trying to get the left position on an element it retur开发者_运维知识库ns nu

im having issue with getting position of an element. there are 4divs with position relative. but and float is left. when im trying to get the left position on an element it retur开发者_运维知识库ns null. in jquery it returns 0. can some one help me please.

Thanks, Dakshina.


This should work.

var el = document.getElementById(<id>);
var left=0, top=0;
do {
  left += el.offsetLeft;
  top += el.offsetTop;
}while(el = el.offsetParent);

// 'left' and 'top' will give you the x,y position.

0

精彩评论

暂无评论...
验证码 换一张
取 消