下面的代码在 Chrome 中运行良好,但在 Firefox 中则不行。警报在 Chrome 中显示适当的边距,而在 Firefox 中则始终显示 0px
。
HTML:
<div class="center">14</div>
CSS:
.center {
margin: 0 auto;
width: 200px;
background-color: #ccc;
}
JQuery: 我正在使用最新的 jQuery 库
$(function(){
var center = $('.center').css('margin-left');
alert(center);
});
请在 Firefox 和 Chrome 中查看 jsfiddle: http://jsfiddle.net/vtbuz/2/
最佳答案
这是一个解决方案:
var center = $('.center').offset().left;
通过使用它,我们可以获得相对于文档的正确左侧位置。
关于jquery - 使用 jquery 获取左边距在 Firefox 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21327718/