简体   繁体   English

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

[英]jQuery - getting position() of the li within ul

I'm trying to figure out how to get the left position of the li relative to its ul parent tag. 我试图弄清楚如何获得li相对于其ul父标记的左侧位置。 Any idea? 任何的想法?

You can make the ul tag the li 's offset parent by giving it position: relative . 您可以通过赋予它position: relative来使ul标记为li的偏移父级。 This will allow you to call position on the li , which will tell you what you want to know. 这将允许你在li上调用position ,这将告诉你你想知道什么。

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 . 看到它在行动

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

It should be possible to get this using jQuery's .position(). 应该可以使用jQuery的.position()来获取它。

$('li').position()

More info here: http://api.jquery.com/position/ 更多信息: http//api.jquery.com/position/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM