gpt4 book ai didi

javascript - 如何让 JavaScript 在加载时执行

转载 作者:行者123 更新时间:2023-11-28 15:18:10 24 4
gpt4 key购买 nike

我尝试过将 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;

}

}

最佳答案

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

<body onload="businessYearlyHours()">

相反。或者完全用 Javascript 来做:

window.onload = businessYearlyHours;

关于javascript - 如何让 JavaScript 在加载时执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32704242/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com