2012-05-10 46 views
2

http://jsfiddle.net/suenot/3b3XM/1/jQuery的偏移()在Firefox 12

// in FF and IE each() don't work offset() 
// $(this).offset().top return empty string 
$(document).ready(function(){ 
    var index = 0; 
    $('.box').each(function(){ 
     var background = $(this).css('background'); 
     $(this).css('background', 'none'); 
     var height = $(this).css('height'); 
     var top = $(this).offset().top; 
     $('body').prepend('<div id="box' + ++index + '"></div>'); 
     $('#box' + index).css({ 
      'height': height, 
      'background': background, 
      'position': 'absolute', 
      'z-index': '-1', 
      'top': top, 
      'width': '100%' 
     }); 
    }); 
});​ 

請幫助我,我無法找到解決方案返回每個空字符串()。

+3

剛剛在FF 12中測試過,它工作正常,'.offset()。top'返回一個值。 –

+0

在每個()測試?在jsfiddle也工作?對不起我的英語不好。 – suenot

+0

請發佈一些代碼片段,而不僅僅是一個鏈接。 – jbabey

回答

2

我不確定$(this).css('background-color');是你想要的,但更新後顯示框。請參見fiddle中的輸出

$(document).ready(function(){ 
    var index = 0; 
    $('.box').each(function(){ 
     var background = $(this).css('background-color'); 
     $(this).css('background', 'none'); 
     var height = $(this).css('height'); 
     var top = $(this).offset().top; 
     $('body').prepend('<div id="box' + ++index + '"></div>'); 
     $('#box' + index).css({ 
      'height': height, 
      'background': background, 
      'position': 'absolute', 
      'z-index': '-1', 
      'top': top, 
      'width': '100%' 
     }); 
    }); 
}); 
+0

+1,這使得輸出在Firefox 12和Chrome 19中相同。 –

+1

@火箭哦我看到'$(this).css('background')'在Chrome中運行良好。 –

+0

這也適用於IE9 :-) –