<html>
<head>
<title>ChangeColorWithDay</title>
<script language="JavaScript">
function focus()
{
var liObjs = document.getElementByTagName("li");
var today = (New Date()).getDay();
for(var i = 0; i < liObjs.length; i++)
{
var liName = liObjs[i].getAttribute("name");
if(("day" + today) == liName)
{
liObjs[i].style.background = "royalblue";
}
}
}
</script>
</head>
<body onload="focus()">
<li name="day0" style="background:tomato;">today is Sunday</li>
<li name="day1">today is Monday</li>
<li name="day2">today is Tuesday</li>
<li name="day3">today is Wednesday</li>
<li name="day4">today is Thursday</li>
<li name="day5">today is Friday</li>
<li name="day6">today is Saturday</li>
</body>
</html>
改的地方兩處:
1:var today = (New Date()).getDay();
//to
var today = (new Date()).getDay();
2:var liObjs = document.getElementByTagName("li");
//to
var liObjs = document.getElementsByTagName("li");