2017-07-06 28 views
-2
jQuery('.webform-component-checkboxes').each(function(){ 
    var height = heightAdjustment(jQuery(this)); 
    console.log(height); 
}); 

function heightAdjustment(variable){ 
    var temp = 0; 
    var elements = {}; 
    var max; 
    jQuery(variable).find('.form-checkboxes .form-type-checkbox label.option').each(function() { 
     max = jQuery(this).parent().css('height'); 
     temp = temp > max ? temp : max ; 
     elements = this;  
    }); 

    elements.parent('div').siblings().css('height',temp); 
} 

我正在使用此代碼,但'loops'的值在循環外部變爲空。但它裏面的工作。

幫幫我!

+0

你剛纔說的,元素只在功能範圍內都有效。 –

+1

您不需要在jQuery標記中重新包裝'variable',因爲您已經將它傳遞給了jQuery對象。另外,'elements'是一個常規對象,不是jQuery元素的集合,所以你的最後一行將會失敗。 – Santi

+0

'元素'應該包含什麼?什麼不起作用? – empiric

回答

-1
  1. 無需重新包裝variable,因爲它已經是一個jQuery對象。
  2. 您的elements是一個常規對象,而不是jQuery元素的集合。如果你想要存儲一個jQuery元素列表,只需將其聲明爲一個變量並將其設置爲一個選擇器即可。
  3. 您正在做console.log(height),但您的功能heightAdjustment()不會返回任何內容。我已經添加了return temp;,以便函數返回設置受影響元素的高度。
var elements; 

jQuery('.webform-component-checkboxes').each(function(){ 
    var height = heightAdjustment(jQuery(this)); 
    console.log(height); 
}); 

function heightAdjustment(variable){ 
    var temp = 0; 
    var max; 
    elements = variable.find('.form-checkboxes .form-type-checkbox label.option'); 
    elements.each(function() { 
     max = jQuery(this).parent().css('height'); 
     temp = temp > max ? temp : max ; 
    }); 

    elements.parent('div').siblings().css('height',temp); 
    return temp; 
} 
+0

之內,但是現在它的給出輸出未定義爲這個, console.log(elements.css('height')); –

+0

因爲'elements'在heightAdjustment();的範圍內聲明,並且不在外部可用。你在哪裏打電話?看到我最新的編輯,也許它解決了你的問題。 – Santi

+0

Downvote未發表評論? :| – Santi