相对定位的TD元素在各浏览器的差异

本文通过一个具体的 HTML 示例,展示了不同浏览器中 offsetParent 属性的行为差异。特别关注了当内部元素采用绝对定位而外部元素采用相对定位时,offsetParent 的指向问题。此问题在 Firefox 4 中表现尤为明显。
<!doctype html> <html> <head> <title>offsetParent bug by 司徒正美</title> <script src="/javascripts/jquery1.52.js"></script> <script src="/neo/dom.js"></script> <style> .test_table{ width: 80%; border: 1px solid #B33F3F; border-collapse: collapse; } .test_td{ border: 1px solid #B33F3F; padding: 5px; position: relative; } .test_div{ position: absolute; border: 1px solid #00cc00; height: 20px; width: auto; } </style> <script> window.onload = function(){ alert(document.getElementById("test_div").offsetParent) } </script> </head> <body> <table class="test_table"> <tbody> <tr> <td class="test_td"> <div class="test_div" id="test_div">测试offsetParent </div> </td> </tr> </tbody> </table> </body> </html>

运行代码

我们对td进行相对定位,它里面的div进行绝对定位,按理div的offsetParent为td,但FF4中则跑到body元素中去了,这搞到我很痛苦,又得套嵌一层了……

/*
opera10.7 td
chrome 10 body
safari5 body
FF3.6 Ff4 body
IE6-IE9 td
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值