jquery position() in plain javascript

谁说我不能喝 提交于 2019-12-10 14:17:13

问题


I have the following line of code:

var newLeftPos = $('span#s' + i).position().left - parseInt($('span#s' + i).css('width'), 10);

It works great in ie6 and upwards but I also need to get it to work for ie5.5. (Let's not argue about that now - I know - but I have no option)

I'm certain it falls on the .position() while I'm testing on ie5.5 with jquery 1.2

How could I do the same in plain javascript? Could "offsetParent" help me here? Apparently ie5.5 supports that.

Thanks in advance.


回答1:


You are looking for offsetParent, offsetLeft and offsetRight.

As you can see in the link, looks like they are supported even by old granny IE5.5.

Check this text page to see if they are really supported by your browser first.

Then your function should be something like

var span = document.getElementById('s' + i);

var newLeftPos = span.offsetWidth - parseInt(span.style.width);


来源:https://stackoverflow.com/questions/7389421/jquery-position-in-plain-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!