innerHTML在for循环中不起作用

时间:2015-08-05 23:47:11

标签: innerhtml

<!DOCTYPE html>
<html>
<head>
<title>Find sunday on jan ist </title>
</head>
<!-- body containing submit button to check and div to display the result-->
<body>
<input type="submit" value="Check" onclick="findSun()" />
<div id="output1">

</div>
</body>
<script>
function findSun()
{
//for loop that checks january 1st on each year from 2015 to 2015 for sunday
for(var year = 2015; year <= 2030; year++)
  {

    var today = new Date(year,0,1);
    var dd = today.getDay();
    if(dd===0)
    {
        var dis = "Jan 1st Of :" + year + "is Sunday";
        console.log(dis);
        document.getElementById('output1').innerHTML = dis;
    }

    else
    {
    document.getElementById('output1').innerHTML= "";

    }
  }

}
</script>
</html>

这是一个简单的程序,从2015年30月开始检查每年1月1日的星期日。我能够在控制台上获得正确的输出,但我无法使用innerHTML在页面上获得正确的输出。我是javascript的新手,任何帮助都得到了认可。

3 个答案:

答案 0 :(得分:0)

每天不匹配时,您正在删除'output1元素'的内容。

如果你想保留以前找到的星期日,你可以在div上附加-with + = - 。

<!DOCTYPE html>
<html>
<head>
<title>Find sunday on jan ist </title>
</head>
<!-- body containing submit button to check and div to display the result-->
<body>
<input type="submit" value="Check" onclick="findSun()" />
<div id="output1">

</div>
</body>
<script>
function findSun()
{
//for loop that checks january 1st on each year from 2015 to 2015 for sunday
for(var year = 2015; year <= 2030; year++)
  {

    var today = new Date(year,0,1);
    var dd = today.getDay();
    if(dd===0)
    {
        var dis = "Jan 1st Of :" + year + "is Sunday";
        console.log(dis);
        document.getElementById('output1').innerHTML += ", " + dis;                                                                                                                                                
    }

    else
    {
    document.getElementById('output1').innerHTML += "\n";

    }
  }

}
</script>
</html>

答案 1 :(得分:0)

你有几个问题,上面提到的第一个问题是你的else语句删除了可以用+ =修复的内容。

第二个问题是你的if语句有太多&#34; =&#34;

<title>Find sunday on jan ist </title>
</head>
<!-- body containing submit button to check and div to display the result-->
<body>
<input type="submit" value="Check" onclick="findSun()" />
<div id="output1">

</div>
</body>
<script>
function findSun()
{
//for loop that checks january 1st on each year from 2015 to 2015 for sunday
for(var year = 2015; year <= 2030; year++)
  {

    var today = new Date(year,0,1);
    var dd = today.getDay();
    if(dd==0)
    {
        var dis = "Jan 1st of: " + year + " is Sunday";
        console.log(dis);
        document.getElementById('output1').innerHTML += dis + "<br>";                                                                                                                                                
    }

  }

}
</script>
</html>

答案 2 :(得分:0)

尝试更改此功能它将完成此任务 问题是你是在编写innerhtml,而不是为什么它会造成问题。

for(var year = 2015; year <= 2030; year++)
 {

var today = new Date(year,0,1);
var dd = today.getDay();
if(dd===0)
{
    var dis = "Jan 1st Of :" + year + "is Sunday";
    console.log(dis);
    document.getElementById('output1').innerHTML += dis + "</br>";
}

else
{
document.getElementById('output1').innerHTML += "";

}
}