使用jquery在Firefox中无法使用margin-left

kp *_*ngh 7 firefox jquery

下面的代码在chrome中工作正常但在firefox中没有.警报显示chrome中的适当边距,而在firefox中它始终显示0px.

HTML:

<div class="center">14</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.center {
    margin: 0 auto;
    width: 200px;
    background-color: #ccc;
}
Run Code Online (Sandbox Code Playgroud)

JQuery:我正在使用最新的jQuery库

$(function(){
    var center = $('.center').css('margin-left');
    alert(center);
});
Run Code Online (Sandbox Code Playgroud)

请参阅firefox和chrome中的jsfiddle:http: //jsfiddle.net/vtbuz/2/

kp *_*ngh 6

这是一个解决方案:

var center = $('.center').offset().left;
Run Code Online (Sandbox Code Playgroud)

通过使用它,我们可以获得相对于文档的正确左侧位置.