这是数组
var weekdayColor = {
sunday : 'red', // sunday
monday : 'blue', // monday
tuesday: 'white', // tuesday
wednesday: 'black',
thursday: 'green',
friday: 'yellow',
saturday: 'orange'
}
我希望能够做一些事情weekcayColor[0]
来获得sunday
:
这是我为面试而写的 JavaScript,已经上交了。我知道有一种更简单的方法可以做到这一点。第一个 varweekdayColor
不能更改;也weekdayColor.sunday
返回red
。
也许我使用了错误的日期方法或错误地访问了 var?
var weekdayColor = {
sunday : 'red', // sunday
monday : 'blue', // monday
tuesday: 'white', // tuesday
wednesday: 'black', // wednessday
thursday: 'green', // thursday
friday: 'yellow', // friday
saturday: 'orange' // saturday
}
var weekday=new Array();
weekday[0]="sunday";
weekday[1]="monday";
weekday[2]="tuesday";
weekday[3]="wednesday";
weekday[4]="thursday";
weekday[5]="friday";
weekday[6]="saturday";
d = new Date;
day = d.getDay();
console.log(weekday[day]);
a = weekday[day];
function change(){
var x = document.getElementById("weekday");
x.innerHTML = a;
x.style.color = weekdayColor[a];
}