jQuery 使用jquery获取div左右值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4228916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:53:02  来源:igfitidea点击:

get div left and right value using jquery

jquery

提问by X10nD

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

谢谢让

回答by Orbling

The .css() command returns what the CSS setting is, so if you have 'auto' set, that is what you will see.

.css() 命令返回 CSS 设置是什么,所以如果你设置了“自动”,这就是你会看到的。

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.

您可以使用 检索相对于父级的计算位置$('#').position().left;,如果您想要相对于文档使用的值,'top' 也可从 position() 获得$('#').offset().left

回答by Vincent Ramdhanie

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.

偏移()方法告诉你元件相对于所述文档的实际位置。请参阅我链接到的页面上的示例,了解他们如何获得左值和上值。

That is:

那是:

  var left = $('Your_selector').offset().left;