not sure if there is a neat way of doing it, but providing the nested tables aren't too busy would it be horribly inefficient to do something like this?
get the prev <a> to <span>, check if <span> is closest to <a>, if false get the prev <a> to the current <a> and test again, if true you have your element.