如何在加载时执行此JavaScript

时间:2015-09-21 20:44:08

标签: javascript html

我已经尝试将onload实现到包含div中,并且当我执行单个部分时,很多代码工作正常,但由于某种原因,即使在jsfiddle中它也不会完全加载:

https://jsfiddle.net/wf6gdr7z/1/

我还有很多要添加到代码中,我更关注它的执行,所以我可以继续测试它并添加功能。我有一种感觉,我错过了一些非常基本的东西。 :)

HTML

<div onload="businessYearlyHours()">

    <h1>Business Hours</h1>
    <p id="businessStatus"></p>
    <p id="businessHours"></p>
    <p id="otherNotes"></p>
    <p>Only Service Animals are allowed in the business</p>

    <p><a href="">Click here to see full hours for the year</a></p>

</div>

JAVASCRIPT

function businessYearlyHours() {

var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();
var hours = d.getHours();
var minutes = d.getMinutes();
var busHours;
var status;    

if (month == 1 || month == 2 || month == 3){    // Jan 1 - Mar 31 Hours

    busHours = "Jan 2-Mar 31: 9AM-5PM"; 
    document.getElementById("businessHours").innerHTML = busHours;

    if(month == 1 && day == 1){
        otherNotes = "Closed Thanksgiving Day and Dec 24-Jan 1";
        document.getElementById("otherNotes").innerHTML = otherNotes; // Display these additional notes on January 1st only

    }

        if (hours >= 9 && hours < 17){
            status = "The Business is Open";        // Consider making this status bold for each entry
            document.getElementByID("businessStatus").innerHTML = status;

        }

        else {
            status = "The Business is Closed";  // Consider making this status bold for each entry
            document.getElementByID("businessStatus").innerHTML = status;

        }

}

else if (month == 4){       // April 1-30 Hours

    busHours = "Apr 1-30: 9AM-7:30PM";
    document.getElementById("businessHours").innerHTML = busHours;

        if (hours >= 9 && (hours < 19 && minutes < 30)){
            status = "The Business is Open";        // Consider making this status bold for each entry
            document.getElementByID("businessStatus").innerHTML = status;

        }

        else {
            status = "The Business is Closed";  // Consider making this status bold for each entry
            document.getElementByID("businessStatus").innerHTML = status;

        }

}

else if (month == 5 || month == 6 || month == 7 || month == 8){     // May 1 - Aug 31 Hours

    busHours = "May 1-Aug 31: 9AM-9PM*"; 
    otherNotes = "*On days when events are scheduled, business hours are 9AM-5PM";
    document.getElementById("businessHours").innerHTML = busHours;
    document.getElementById("otherNotes").innerHTML = otherNotes;

        if (hours >= 9 && hours < 21){
            status = "The Business is Open";        // Consider making this status bold for each entry
            document.getElementByID("businessStatus").innerHTML = status;

        }

        else {
            status = "The Business is Closed";  // Consider making this status bold for each entry
            document.getElementByID("businessStatus").innerHTML = status;

        }

}

else if (month == 9) {  // Sep 1 - 30 Hours

    busHours = "Sep 1-30: 9AM-7:30PM*";
    otherNotes = "*On days when events are scheduled, business hours are 9AM-5PM";
    document.getElementById("businessHours").innerHTML = busHours;
    document.getElementById("otherNotes").innerHTML = otherNotes;

        if (hours >= 9 && (hours >= 9 && (hours < 19 && minutes < 30)){
            status = "The Business is Open";        // Consider making this status bold for each entry
            document.getElementByID("businessStatus").innerHTML = status;

        }

        else {
            status = "The Business is Closed";  // Consider making this status bold for each entry
            document.getElementByID("businessStatus").innerHTML = status;

        }

}

// Closed Thanksgiving Day / Christmas

else if ( month == 10 || month == 11 || month == 12) {      

    busHours = "Oct 1-Dec 23: 9AM-5PM";
    otherNotes = "Closed Thanksgiving Day and Dec 24-Jan 1"; 
    document.getElementById("businessHours").innerHTML = busHours;
    document.getElementById("otherNotes").innerHTML = otherNotes;

        if (hours >= 9 && hours < 17){
            status = "The Business is Open";        // Consider making this status bold for each entry
            document.getElementByID("businessStatus").innerHTML = status;

        }

        else {
            status = "The Business is Closed";  // Consider making this status bold for each entry
            document.getElementByID("businessStatus").innerHTML = status;

        }

}

else () {

    break;

}

}

2 个答案:

答案 0 :(得分:3)

DIV个元素不会触发load事件。使用

<body onload="businessYearlyHours()">

代替。或者完全用Javascript完成:

window.onload = businessYearlyHours;

答案 1 :(得分:1)

您的A标签未链接到您的功能。您应该在click事件上绑定处理函数。