开发者

position is not working when the containing element is inside table - jquery

开发者 https://www.devze.com 2023-04-13 04:23 出处:网络
When my containing element(div) is inside <td>, and If i use position(), It is not re开发者_JAVA百科turning the position relative to td. instead it returns position relative to <tr>.

When my containing element(div) is inside <td>, and If i use position(), It is not re开发者_JAVA百科turning the position relative to td. instead it returns position relative to <tr>.

Why this is happening? Any workaround for this?


position() returns the offset difference between the current element, and the closest offset parent.

If you want to get the position relative to the direct parent, use:

var element = $("div"); //Selector
var offset = element.offset().top - element.parent().offset().top;


You need to set the position CSS property of the <td> to relative:
In your css file:

table td { position:relative; }
0

精彩评论

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

关注公众号