如何在加载时执行此JavaScript

如何在加载时执行此JavaScript,javascript,html,Javascript,Html,我已经尝试将onload实现到包含div中,当我执行单个片段时,很多代码都可以正常工作,但由于某些原因,即使在jsfiddle中,它也不会完全加载: 我还有很多东西要添加到代码中,我更关心它的执行,这样我就可以继续测试它并添加功能。我觉得我错过了一些非常基本的东西 HTML <div onload="businessYearlyHours()"> <h1>Business Hours</h1> <p id="businessStatu

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

我还有很多东西要添加到代码中,我更关心它的执行,这样我就可以继续测试它并添加功能。我觉得我错过了一些非常基本的东西

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;

}

}
函数businessYearlyHours(){
var d=新日期();
变量月份=d.getMonth()+1;
var day=d.getDate();
var hours=d.getHours();
var minutes=d.getMinutes();
var busHours;
var状态;
如果(月==1 | |月==2 | |月==3){//1月1日-3月31小时
布什小时=“1月2日至3月31日:上午9点至下午5点”;
document.getElementById(“businessHours”).innerHTML=busHours;
如果(月=1日=1){
otherNotes=“感恩节和12月24日至1月1日结束”;
document.getElementById(“otherNotes”).innerHTML=otherNotes;//仅在1月1日显示这些附加注释
}
如果(小时数>=9和小时数<17){
状态=“业务是开放的”;(或)考虑为每个条目使这种状态变得大胆。
document.getElementByID(“businessStatus”).innerHTML=status;
}
否则{
状态=“业务已关闭”;(或)考虑为每个条目使该状态变为粗体
document.getElementByID(“businessStatus”).innerHTML=status;
}
}
如果(月==4){//4月1-30小时
busHours=“4月1日至30日上午9时至下午7时30分”;
document.getElementById(“businessHours”).innerHTML=busHours;
如果(小时>=9&(小时<19&&分钟<30)){
状态=“业务是开放的”;(或)考虑为每个条目使这种状态变得大胆。
document.getElementByID(“businessStatus”).innerHTML=status;
}
否则{
状态=“业务已关闭”;(或)考虑为每个条目使该状态变为粗体
document.getElementByID(“businessStatus”).innerHTML=status;
}
}
如果(月==5 | |月==6 | |月==7 | |月==8){//5月1日至8月31日小时
busHours=“5月1日至8月31日:上午9点至晚上9点*”;
otherNotes=“*在安排活动的日子,营业时间为上午9点至下午5点”;
document.getElementById(“businessHours”).innerHTML=busHours;
document.getElementById(“otherNotes”).innerHTML=otherNotes;
如果(小时数>=9和小时数<21){
状态=“业务是开放的”;(或)考虑为每个条目使这种状态变得大胆。
document.getElementByID(“businessStatus”).innerHTML=status;
}
否则{
状态=“业务已关闭”;(或)考虑为每个条目使该状态变为粗体
document.getElementByID(“businessStatus”).innerHTML=status;
}
}
如果(月==9){//9月1日-30小时
busHours=“9月1日至30日上午9时至下午7时30分*”;
otherNotes=“*在安排活动的日子,营业时间为上午9点至下午5点”;
document.getElementById(“businessHours”).innerHTML=busHours;
document.getElementById(“otherNotes”).innerHTML=otherNotes;
如果(小时>=9&&(小时>=9&&(小时<19&&分钟<30)){
状态=“业务是开放的”;(或)考虑为每个条目使这种状态变得大胆。
document.getElementByID(“businessStatus”).innerHTML=status;
}
否则{
状态=“业务已关闭”;(或)考虑为每个条目使该状态变为粗体
document.getElementByID(“businessStatus”).innerHTML=status;
}
}
//感恩节/圣诞节休息
如果(月=10 | |月=11 | |月=12){
布什小时=“10月1日至12月23日:上午9点至下午5点”;
otherNotes=“感恩节和12月24日至1月1日结束”;
document.getElementById(“businessHours”).innerHTML=busHours;
document.getElementById(“otherNotes”).innerHTML=otherNotes;
如果(小时数>=9和小时数<17){
状态=“业务是开放的”;(或)考虑为每个条目使这种状态变得大胆。
document.getElementByID(“businessStatus”).innerHTML=status;
}
否则{
状态=“业务已关闭”;(或)考虑为每个条目使该状态变为粗体
document.getElementByID(“businessStatus”).innerHTML=status;
}
}
else(){
打破
}
}

您的A标记没有链接到您的函数。您应该在单击事件上绑定处理程序函数。

您的A标记没有链接到您的函数。您应该在单击事件上绑定处理程序函数。

DIV
元素不会触发
加载
事件。请使用

<body onload="businessYearlyHours()">

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

<body onload="businessYearlyHours()">

代码中存在语法错误。保持浏览器的JavaScript控制台打开,您将在那里看到错误消息。谢谢。我修复了两个不同的语法错误,Chrome的控制台不再显示错误。它仍然不会执行代码来填充部分。您是否在某处显式调用您的函数?问题不包含该p艺术。div没有onload事件。使用
而不是
代码中有语法错误。保持浏览器的JavaScript控制台打开,您将在那里看到错误消息。谢谢。我修复了两个不同的语法错误,Chrome的控制台不再显示错误。它仍然不会执行要填充的代码部分。是否在某个地方显式调用函数?问题不包含该部分。div没有onload事件。请使用
而不是
,这可能是真的,但与que无关