[英]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");
比较.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.