开发者社区> 问答> 正文

为什么下面JavaScript代码没有达到效果:根据当前星期改变标签<li>的背景颜色?

<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>

展开
收起
小旋风柴进 2016-03-17 10:13:26 2155 0
1 条回答
写回答
取消 提交回答
  • New Date ,New 小写 new

    2019-07-17 19:04:36
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
JavaScript函数 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载