2013-05-01 53 views
0

工作在這個代碼,我相當卡住..我想要實現的是,當選擇div中有多個2選擇時,覆蓋消息的底部位置從10px(在CSS中定義)移動到90px(定義在jQuery中)。問題在於它應用於所有覆蓋消息(90px),即使在記錄結果時,我會將2,3和4作爲單獨的結果。爲什麼不是將結果單獨應用於每個包裝div?這不是每個功能的作用,是應用在每個單獨的?在這個例子中如何正確使用每個函數?

謝謝!

演示:http://jsfiddle.net/6qD52/

代碼:

$(".overlay").each(function() { 
    selection_number = $(this).parent().find(".selections").find(".selection").length; 
    console.log(selection_number); 

    if ((selection_number) > 2) { 
     $(".overlay_message").css({bottom: 90}); 
    } 
}); 

回答

3

$('.overlay_message')選擇覆蓋消息所有。你希望你的元素的一個內:

$(this).find(".overlay_message").css({bottom: 90}); 
+0

感謝您的澄清和解決方案攪拌機,它應該的確如此! – Yunowork 2013-05-01 18:22:45

3

您要選擇的.overlay_message你在操作的元素:

$(this).find('.overlay_message') 
+0

謝謝,作品,答案與上面相同。 – Yunowork 2013-05-01 18:23:07

2

試試這個:

// Selects the 'overlay_message' div inside the current 'overlay' div 
$(this).find(".overlay_message").css({bottom: 90}); 

取而代之:

// Selects all the 'overlay_message' div 
$(".overlay_message").css({bottom: 90}); 
+0

感謝Palash,現在作品! – Yunowork 2013-05-01 18:23:28

相關問題