There is no left assigned for the div, I use $('#').css('left');
to get the left value, but it shows auto.
没有为div分配左边,我使用$('#')。css('left');获取左值,但它显示自动。
How do I get the left value of the div.
如何获取div的左侧值。
Thanks Jean
2 个解决方案
#1
18
The .css() command returns what the CSS setting is, so if you have 'auto' set, that is what you will see.
.css()命令返回CSS设置的内容,因此如果您设置了'auto',那么您将看到。
You can retrieve the computed position relative to the parent with $('#').position().left;
, 'top' also is available from position(), if you want the value relative to the document use $('#').offset().left
.
您可以使用$('#')检索相对于父项的计算位置。位置()。左;如果您希望相对于文档的值使用$('#,'top'也可以从position()获得“).offset()。左侧。
#2
3
The offset() method tells you the actual position of the element relative to the document. See the example on the page that I linked to to see how they got the left and top values.
offset()方法告诉您元素相对于文档的实际位置。请参阅我链接到的页面上的示例,以了解它们如何获得左侧和顶部值。
That is:
var left = $('Your_selector').offset().left;
#1
18
The .css() command returns what the CSS setting is, so if you have 'auto' set, that is what you will see.
.css()命令返回CSS设置的内容,因此如果您设置了'auto',那么您将看到。
You can retrieve the computed position relative to the parent with $('#').position().left;
, 'top' also is available from position(), if you want the value relative to the document use $('#').offset().left
.
您可以使用$('#')检索相对于父项的计算位置。位置()。左;如果您希望相对于文档的值使用$('#,'top'也可以从position()获得“).offset()。左侧。
#2
3
The offset() method tells you the actual position of the element relative to the document. See the example on the page that I linked to to see how they got the left and top values.
offset()方法告诉您元素相对于文档的实际位置。请参阅我链接到的页面上的示例,以了解它们如何获得左侧和顶部值。
That is:
var left = $('Your_selector').offset().left;