JavaScript onload函数未执行

JavaScript onload函数未执行,javascript,function,Javascript,Function,我有一个问题,让我们直截了当地说,我的函数没有执行 <h1>Hello World!</h1> <div id="testDate">Oeps...</div> <script> var season5 = [ "2014,09,13,22,00,00", //1 "2014,09,20,22,00,00", //2 "2014,09,27,22,00,00", //3 "2014,10,3,22,00,00", //4 "20

我有一个问题,让我们直截了当地说,我的函数没有执行

<h1>Hello World!</h1>
<div id="testDate">Oeps...</div>
<script>
    var season5 = [
"2014,09,13,22,00,00", //1
"2014,09,20,22,00,00", //2
"2014,09,27,22,00,00", //3
"2014,10,3,22,00,00", //4
"2014,10,10,22,00,00", //5
"2014,10,17,22,00,00", //6
"2014,10,24,22,00,00", //7
"2014,11,2,22,00,00", //8
//MidSeason break
"2015,01,9,22,00,00", //9
"2015,01,16,22,00,00", //10
"2015,01,23,22,00,00", //11
"2015,02,2,22,00,00", //12
"2015,02,9,22,00,00", //13
"2015,02,16,22,00,00", //14
"2015,02,23,22,00,00", //15
"2015,02,30,22,00,00", //16
];
function compareDate() {
    alert("What do you mean it doesn't run??");
    console.log("Function fired");

    var today = new Date();
    var todayDate = today.getFullYear() + "," + today.getMonth() + "," + today.getDay() + "," + today.getHours() + "," + today.getMinutes() + "," + today.getSeconds();
    if (Date.parse(todayDate) > Date.parse(season5[0])) {
        document.getElementById("testDate").innerHTML = "111";
        console.log("111");
    } else {
        document.getElementById("testDate").innerHTML = "222";
        console.log("222");
    }
}
</script>

</body>
</html>
代码:

<h1>Hello World!</h1>
<div id="testDate">Oeps...</div>
<script>
    var season5 = [
"2014,09,13,22,00,00", //1
"2014,09,20,22,00,00", //2
"2014,09,27,22,00,00", //3
"2014,10,3,22,00,00", //4
"2014,10,10,22,00,00", //5
"2014,10,17,22,00,00", //6
"2014,10,24,22,00,00", //7
"2014,11,2,22,00,00", //8
//MidSeason break
"2015,01,9,22,00,00", //9
"2015,01,16,22,00,00", //10
"2015,01,23,22,00,00", //11
"2015,02,2,22,00,00", //12
"2015,02,9,22,00,00", //13
"2015,02,16,22,00,00", //14
"2015,02,23,22,00,00", //15
"2015,02,30,22,00,00", //16
];
function compareDate() {
    alert("What do you mean it doesn't run??");
    console.log("Function fired");

    var today = new Date();
    var todayDate = today.getFullYear() + "," + today.getMonth() + "," + today.getDay() + "," + today.getHours() + "," + today.getMinutes() + "," + today.getSeconds();
    if (Date.parse(todayDate) > Date.parse(season5[0])) {
        document.getElementById("testDate").innerHTML = "111";
        console.log("111");
    } else {
        document.getElementById("testDate").innerHTML = "222";
        console.log("222");
    }
}
</script>

</body>
</html>

我得再打些文字之类的。。。我还需要再写一些…

问题可能出在其他地方。我创建这个提琴是为了向您展示这个函数执行得很好

<h1>Hello World!</h1>
<div id="testDate">Oeps...</div>
<script>
    var season5 = [
"2014,09,13,22,00,00", //1
"2014,09,20,22,00,00", //2
"2014,09,27,22,00,00", //3
"2014,10,3,22,00,00", //4
"2014,10,10,22,00,00", //5
"2014,10,17,22,00,00", //6
"2014,10,24,22,00,00", //7
"2014,11,2,22,00,00", //8
//MidSeason break
"2015,01,9,22,00,00", //9
"2015,01,16,22,00,00", //10
"2015,01,23,22,00,00", //11
"2015,02,2,22,00,00", //12
"2015,02,9,22,00,00", //13
"2015,02,16,22,00,00", //14
"2015,02,23,22,00,00", //15
"2015,02,30,22,00,00", //16
];
function compareDate() {
    alert("What do you mean it doesn't run??");
    console.log("Function fired");

    var today = new Date();
    var todayDate = today.getFullYear() + "," + today.getMonth() + "," + today.getDay() + "," + today.getHours() + "," + today.getMinutes() + "," + today.getSeconds();
    if (Date.parse(todayDate) > Date.parse(season5[0])) {
        document.getElementById("testDate").innerHTML = "111";
        console.log("111");
    } else {
        document.getElementById("testDate").innerHTML = "222";
        console.log("222");
    }
}
</script>

</body>
</html>
代码:

<h1>Hello World!</h1>
<div id="testDate">Oeps...</div>
<script>
    var season5 = [
"2014,09,13,22,00,00", //1
"2014,09,20,22,00,00", //2
"2014,09,27,22,00,00", //3
"2014,10,3,22,00,00", //4
"2014,10,10,22,00,00", //5
"2014,10,17,22,00,00", //6
"2014,10,24,22,00,00", //7
"2014,11,2,22,00,00", //8
//MidSeason break
"2015,01,9,22,00,00", //9
"2015,01,16,22,00,00", //10
"2015,01,23,22,00,00", //11
"2015,02,2,22,00,00", //12
"2015,02,9,22,00,00", //13
"2015,02,16,22,00,00", //14
"2015,02,23,22,00,00", //15
"2015,02,30,22,00,00", //16
];
function compareDate() {
    alert("What do you mean it doesn't run??");
    console.log("Function fired");

    var today = new Date();
    var todayDate = today.getFullYear() + "," + today.getMonth() + "," + today.getDay() + "," + today.getHours() + "," + today.getMinutes() + "," + today.getSeconds();
    if (Date.parse(todayDate) > Date.parse(season5[0])) {
        document.getElementById("testDate").innerHTML = "111";
        console.log("111");
    } else {
        document.getElementById("testDate").innerHTML = "222";
        console.log("222");
    }
}
</script>

</body>
</html>

<h1>Hello World!</h1>
<div id="testDate">Oeps...</div>
<script>
    var season5 = [
"2014,09,13,22,00,00", //1
"2014,09,20,22,00,00", //2
"2014,09,27,22,00,00", //3
"2014,10,3,22,00,00", //4
"2014,10,10,22,00,00", //5
"2014,10,17,22,00,00", //6
"2014,10,24,22,00,00", //7
"2014,11,2,22,00,00", //8
//MidSeason break
"2015,01,9,22,00,00", //9
"2015,01,16,22,00,00", //10
"2015,01,23,22,00,00", //11
"2015,02,2,22,00,00", //12
"2015,02,9,22,00,00", //13
"2015,02,16,22,00,00", //14
"2015,02,23,22,00,00", //15
"2015,02,30,22,00,00", //16
];
function compareDate() {
    alert("What do you mean it doesn't run??");
    console.log("Function fired");

    var today = new Date();
    var todayDate = today.getFullYear() + "," + today.getMonth() + "," + today.getDay() + "," + today.getHours() + "," + today.getMinutes() + "," + today.getSeconds();
    if (Date.parse(todayDate) > Date.parse(season5[0])) {
        document.getElementById("testDate").innerHTML = "111";
        console.log("111");
    } else {
        document.getElementById("testDate").innerHTML = "222";
        console.log("222");
    }
}
</script>

</body>
</html>

试试这个,它对我很有用

这是工作代码,做了一些修复,比如我们需要使用getDate而不是getDay,today.getMonth+1和handled season5[0]

<h1>Hello World!</h1>
<div id="testDate">Oeps...</div>
<script>
    var season5 = [
"2014,09,13,22,00,00", //1
"2014,09,20,22,00,00", //2
"2014,09,27,22,00,00", //3
"2014,10,3,22,00,00", //4
"2014,10,10,22,00,00", //5
"2014,10,17,22,00,00", //6
"2014,10,24,22,00,00", //7
"2014,11,2,22,00,00", //8
//MidSeason break
"2015,01,9,22,00,00", //9
"2015,01,16,22,00,00", //10
"2015,01,23,22,00,00", //11
"2015,02,2,22,00,00", //12
"2015,02,9,22,00,00", //13
"2015,02,16,22,00,00", //14
"2015,02,23,22,00,00", //15
"2015,02,30,22,00,00", //16
];
function compareDate() {
    alert("What do you mean it doesn't run??");
    console.log("Function fired");

    var today = new Date();
    var todayDate = today.getFullYear() + "," + today.getMonth() + "," + today.getDay() + "," + today.getHours() + "," + today.getMinutes() + "," + today.getSeconds();
    if (Date.parse(todayDate) > Date.parse(season5[0])) {
        document.getElementById("testDate").innerHTML = "111";
        console.log("111");
    } else {
        document.getElementById("testDate").innerHTML = "222";
        console.log("222");
    }
}
</script>

</body>
</html>

为什么要创建一个日期,将其转换为字符串,然后再次解析它?现在就使用Date.now。这里没有任何东西可以解释为什么函数没有被调用。您需要发布一个实际显示问题的示例。我在同一个脚本标记中有另一个console.log,该文件正在打印到控制台上,如@Juhana所说,发布更多代码以便我们进一步帮助您。这就是您的全部代码?html标签在哪里?更新了代码,效果很好。控制台记录得很好,修复了一些错误,但它仍然没有执行:你试过小提琴吗?