2014-04-18 137 views
0

基本上,我試圖:獲取父母的身高,減去孩子的身高,將結果除以2,然後使用結果作爲孩子上邊距的值。它工作正常,但只在第一個div。其他人獲得相同的價值。jQuery each()得到相同的結果

這裏是jQuery的:

function update() 
{ 
    var container = $('.container'); 
    var inside = $('.inside'); 

    inside.each(function() 
    { 
     var cont_height = container.outerHeight(); 
     var inside_height = inside.outerHeight(); 
     var x = (cont_height - inside_height)/2, 
      x = Math.floor(x); 
     $(this).css("marginTop", x); 
    }); 
} 

$(document).ready(function() 
{ 
    update(); 
}); 

$(window).resize(function() 
{ 
    update(); 
}); 

我提前道歉,如果這已經回答過了。我試圖看,但我似乎無法拿出正確的關鍵字。

這裏的小提琴:http://jsfiddle.net/y29S4/4/

非常感謝你。

回答

1

您inside_height作爲內部定義和它在全球範圍內,所以只需要第一個元素,你需要改變代碼:

var inside_height = $(this).outerHeight(); 
+0

你是真棒。非常感謝你。 – Catcher

相關問題