2011-09-14 60 views

回答

6

您可以通过提供position: relative来使ul标记li的抵消父项。这将允许你拨打liposition,这将告诉你你想知道什么。

var pos = $("#target").position(); 
$("#target").text("This li is positioned " + pos.top 
       + "px from the top and " + pos.left 
       + "px from the left edge of the ul"); 

See it in action

+0

感谢 - 这似乎在做的伎俩! – user398341

1

比较li.offset()ul.parent().offset()

0

应该可以使用jQuery的.position()来获取它。

$('li').position() 

此处了解详情: http://api.jquery.com/position/

+0

是的 - 但我想要得到李的位置,而不是ul标签 – user398341

+0

哈,对不起,我打算写$('li'),更新了我的答案! – brain

+0

@Jon已经提供了答案 - li本身没有在ul标签上的相对位置将返回相对于文档而不是ul的位置。 – user398341