2011-09-14 114 views
1

我不得不求助於此bodge設置div01-05不同的高度,以div11-15 :(迭代的div陣列

var maxheight = divheight; 
var dayofweek = <?echo date("w",strtotime($today));?>; 
var heightoffset = (2 - dayofweek) * 40; 

var days = jQuery('div.day01, div.day02, div.day03, div.day04, div.day05'); 
for (i=0; i<days.length; i++){ 
    var hh = jQuery(days[i]).prop("scrollHeight"); 
    if (hh > maxheight) { 
     maxheight = hh; 
    }; 
    jQuery(days[i]).height(divheight+heightoffset); 
}; 

var days = jQuery('div.day11, div.day12, div.day13, div.day14, div.day15'); 
for (i=0; i<days.length; i++){ 
    var hh = jQuery(days[i]).prop("scrollHeight"); 
    if (hh > maxheight) { 
     maxheight = hh; 
    }; 
    jQuery(days[i]).height(divheight-heightoffset); 
}; 

//Back to full array for further processing 
var days = jQuery('div.day01, div.day02, div.day03, div.day04, div.day05, div.day11, div.day12, div.day13, div.day14, div.day15'); 

會有人請把我趕出痛苦和秀我如何迭代所有的div並做一個有條件的設置高度

一個警告是,PHP是greating的div和div.day01 -04可能不存在取決於一天的一天div.day05總是會和so div div day11-15

關於 Simon

最終代碼的結果(已經更正爲使用ID而不是類:)

jQuery('div[id^="day"]').each(function() { 
     var hh = jQuery(this).prop("scrollHeight"); 
     if (hh > maxheight) {maxheight = hh;}; 
     var cn = jQuery(this).attr('id').split(" ")[0]; // Since PHP is creating these classes, we can safely assume "day" will always be the first. 
     var num = parseInt(cn.slice(-2),10); // extract the last two characters and convert to an integer 
     if (num >=1 && num <= 5) { 
      jQuery(this).height(divheight+heightoffset); 
     }else if(num >=10 && num <= 15){ 
      jQuery(this).height(divheight-heightoffset); 
     } 
    }); 

回答

2
$('div[class^="day"]').each(function() { 
    var cn = $(this).attr('class').split(" ")[0]; // Since PHP is creating these classes, we can safely assume "day" will always be the first. 
    var num = parseInt(cn.slice(-2),10); // extract the last two characters and convert to an integer 
    if (num >=1 && num <= 5) { 
    // Working with div 01-05 
    }else if(num >=10 && num <= 15){ 
    // Working with div 11-15 
    } 
}); 
+1

'div's可能包含其他類,所以你應該檢查,看看是否div包含類'/ day \ d * /'第一個。 –

+1

這就是爲什麼我應該使用ID代替。:-) – Blazemonger

+0

重新您的$ div每個函數 - 如何我改變這個只是迭代div的01-15,因爲我還有其他的div,感謝在課堂上的頭像/編號錯誤 - 我會明確地避開它,但會糾正:) – SimpleSi

1

遍歷網頁上的所有div,使用

jQuery('div').each(function(d) { 
    //This code will run for each div on the page 
    //Just put any conditionals in here 
    //Use jQuery(this) to refer to the current div 
}); 

編輯:

使用jQuery(this).attr('name')訪問當前元素的名稱。使用jQuery(this).attr('id')訪問當前元素的ID。

使用jQuery(this).hasClass('classname')(布爾值)檢查當前元素是否具有指定的類。

+0

對不起 - 還有其他的div所以代碼必須只適用於days01..days15,我不知道如何訪問div的名字被加工做有條件的測試 - 多數民衆贊成我的問題:( – SimpleSi

+0

@SimpleSi這是在我的答案描述,我現在追加更多的細節 – yoozer8

1

您可以在頁面上使用$('div')遍歷每個div,然後使用正則表達式來獲得你想要的div秒。您甚至可以使用[class*="day"]來限制div的jQuery循環。

$('div[class*="day"]').each(function(){ 
    var dayClass = this.className.match(/day\d*/); 
    if(dayClass != null){ // if the div contains the class day.. 
    var dayID = dayClass[0].replace('day', ''); // 01, 02, 03, etc. 
    if(dayID >=1 && dayID <= 5){ 
    } 
    else if(dayID >= 11 && dayID <= 15){ 
    } 
    else{ 
    } 
    } 
}); 

演示:http://jsfiddle.net/9kxSF/1/

這裏一個更好的想法是把所有的div SA類說「dayDiv」,還有就是「day01」或「day02」類,所以,那麼你可以做$('div.dayDiv')得到你想要的div

<div class="dayDiv day01"></div> 
<div class="dayDiv day02"></div> 

然後在jQuery的:

$('div.dayDiv').each(function(){ 
    var dayClass = this.className.match(/day\d*/); 
    var dayID = dayClass[0].replace('day', ''); // 01, 02, 03, etc. 
    if(dayID >=1 && dayID <= 5){ 
    } 
    else if(dayID >= 11 && dayID <= 15){ 
    } 
    else{ 
    } 
}); 
+0

感謝您的建議 - 我沒有意識到您可以給多個「名稱」div div – SimpleSi

+0

@SimpleSi:您可以給元素多個類,但不是名稱或ID。 –