现在,我正在使用javascript开发时间轴程序。当我单击每个班级时,由于每个班级均为2小时课程,因此应将其着色为带有2个块的时间表。
每个时间表表都有id(例如,星期一的第一堂课有=“ mon1”,而生日的第二堂课有'fri2'
这是我的代码。而且效果很好。但是我想知道如何减少代码。
let timelineColor = getRamdomColor();
let clickDay;
let clickDay1;
for (i = 0; i < dayArray.length; i++) {
if (dayArray[i] === 'monday') {
clickDay = 'mon' + timeArray[i];
let table = document.getElementById(clickDay);
table.style.backgroundColor = timelineColor;
clickDay1 = 'mon' + (Number(timeArray[i]) + 1);
console.log(clickDay1);
let table1 = document.getElementById(clickDay1);
table1.style.backgroundColor = timelineColor;
} else if (dayArray[i] === 'tuesday') {
clickDay = 'tue' + timeArray[i];
let table = document.getElementById(clickDay);
table.style.backgroundColor = timelineColor;
clickDay1 = 'tue' + (Number(timeArray[i]) + 1);
let table1 = document.getElementById(clickDay1);
table1.style.backgroundColor = timelineColor;
} else if (dayArray[i] === 'wednesday') {
clickDay = 'wed' + timeArray[i];
let table = document.getElementById(clickDay);
table.style.backgroundColor = timelineColor;
clickDay1 = 'wed' + (Number(timeArray[i]) + 1);
let table1 = document.getElementById(clickDay1);
table1.style.backgroundColor = timelineColor;
} else if (dayArray[i] === 'thursday') {
clickDay = 'tur' + timeArray[i];
let table = document.getElementById(clickDay);
table.style.backgroundColor = timelineColor;
clickDay1 = 'tur' + (Number(timeArray[i]) + 1);
let table1 = document.getElementById(clickDay1);
table1.style.backgroundColor = timelineColor;
} else if (dayArray[i] === 'friday') {
clickDay = 'fri' + timeArray[i];
let table = document.getElementById(clickDay);
table.style.backgroundColor = timelineColor;
clickDay1 = 'fri' + (Number(timeArray[i]) + 1);
let table1 = document.getElementById(clickDay1);
table1.style.backgroundColor = timelineColor;
}
}
答案 0 :(得分:2)
您根本不需要if
语句。设置一个将您的全天名称与短日名称相关联的地图,然后进行查找:
let timelineColor = getRamdomColor();
let clickDay;
let clickDay1;
var dayMap = {
'monday' : 'mon',
'tuesday' : 'tue',
'wednesday': 'wed',
'thursday' : 'tur',
'friday' : 'fri'
};
for (var i = 0; i < dayArray.length; i++) {
let shortDayName = dayMap[dayArray[i]];
clickDay = shortDayName + timeArray[i];
let table = document.getElementById(clickDay);
table.style.backgroundColor = timelineColor;
clickDay1 = shortDayName + (Number(timeArray[i]) + 1);
console.log(clickDay1);
let table1 = document.getElementById(clickDay1);
table1.style.backgroundColor = timelineColor;
}
答案 1 :(得分:1)
您可以使用switch
语句,而不要使用多个if-elseif
语句:
let timelineColor = getRamdomColor();
for (i = 0; i < dayArray.length; i++){
let day;
switch (dayArray[i]){
case 'monday': day = 'mon'; break;
case 'tuesday': day = 'tue'; break;
case 'wednesday': day = 'wed'; break;
case 'thursday': day = 'tur'; break;
case 'friday': day = 'fri'; break;
}
// I moved these variables into the loop too
let clickDay = day + timeArray[i];
let clickDay1 = day + (Number(timeArray[i]) + 1);
let table = document.getElementById(clickDay);
let table1 = document.getElementById(clickDay1);
table.style.backgroundColor = timelineColor;
table1.style.backgroundColor = timelineColor;
}