gpt4 book ai didi

javascript - 遍历 div 数组

转载 作者:行者123 更新时间:2023-11-30 10:47:28 24 4
gpt4 key购买 nike

我不得不求助于此 bodge 来设置 div01-05 的高度不同于 div11-15 :(

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 正在改进 div,div.day01 -04 可能不存在,具体取决于星期几 div.day05 始终如此,divs day11-15 也是如此

问候西蒙

最终代码结果(已更正为使用 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);
}
});

最佳答案

$('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
}
});

关于javascript - 遍历 div 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7417543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com