jQuery - 在ul中获取li的position()

use*_*341 3 jquery position

我试图弄清楚如何获得li相对于其ul父标记的左侧位置.任何的想法?

Jon*_*Jon 6

您可以通过提供ul标记使标记成为li偏移父项position: relative.这将允许你打电话positionli,它会告诉你,你想知道什么.

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");
Run Code Online (Sandbox Code Playgroud)

看到它在行动.