Getting offsetTop of an element in a table

I cannot figure out how to get the offsetTop of an element inside a table. It works great on elements outside the tables, but all elements inside the table return the same result and are usually located at the top of the page. I tried this in Firefox and Chrome. How to get offsetTop of an element in a table?

+5
source share
1 answer

offsetTopreturns a value relative to offsetParent; you need to recursively add offsetParent.offsetTopacross all parents until offsetParentit is null. Consider using the jQuery method.offset ()

EDIT. jQuery, ():

function offset(elem) {
    if(!elem) elem = this;

    var x = elem.offsetLeft;
    var y = elem.offsetTop;

    while (elem = elem.offsetParent) {
        x += elem.offsetLeft;
        y += elem.offsetTop;
    }

    return { left: x, top: y };
}
+17

All Articles