2011-09-14 180 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。 –