我正在尝试根据网页的日期动态更新网页上的文字。但由于某种原因,文本周三上午9点到下午5点半开放#34;没有出现。我正在使用XAMPP,但我不认为这很重要。她是我的代码。
var hours = ["Closed", "Closed", "9 AM to 9 PM", "9 AM to 5:30 PM","9 AM to 9 PM", "9 AM to 5:30 PM", "9 AM to 2:30 PM"]; //Sunday -> Saturday
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; // Variable to Convert getDay() to text
var dayOfWeek = getDay();
document.getElementById("open").innerHTML = "OPEN " + days[dayOfWeek] + " FROM " + days[dayOfWeek];
和
<p id="open"></p>
答案 0 :(得分:1)
我认为你缺少一些东西
运行代码段以获取更多详细信息
var hours = ["Closed", "Closed", "9 AM to 9 PM", "9 AM to 5:30 PM","9 AM to 9 PM", "9 AM to 5:30 PM", "9 AM to 2:30 PM"]; //Sunday -> Saturday
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; // Variable to Convert getDay() to text
var d = new Date();
var dayOfWeek = d.getDay();
document.getElementById("open").innerHTML = "OPEN " + days[dayOfWeek] + " FROM " + hours[dayOfWeek];
&#13;
<p id="open"></p>
&#13;
答案 1 :(得分:0)
如何定义getDay()函数?
如果未定义,请使用Date对象:
var dayOfWeek = new Date().getDay();
http://jsfiddle.net/trestqwz/1/
var hours = ["Closed", "Closed", "9 AM to 9 PM", "9 AM to 5:30 PM","9 AM to 9 PM", "9 AM to 5:30 PM", "9 AM to 2:30 PM"]; //Sunday -> Saturday
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; // Variable to Convert getDay() to text
var dayOfWeek = new Date().getDay();
document.getElementById("open").innerHTML = "OPEN " + days[dayOfWeek] + " FROM " + hours[dayOfWeek];
答案 2 :(得分:0)
除了adeno的评论之外,您还需要:
a)将代码放在 p
元素之后的文档中
要么
b)在文档加载后运行代码。
我将介绍后一种方法的一个例子。
<!DOCTYPE html>
<html>
<head>
<script>
"use strict";
function byId(e){return document.getElementById(e);}
function newEl(tag){return document.createElement(tag);}
function newTxt(txt){return document.createTextNode(txt);}
function toggleClass(elem, className){elem.classList.toggle(className);}
function toggleClassById(targetElemId, className){byId(targetElemId).classList.toggle(className)}
function hasClass(elem, className){return elem.classList.contains(className);}
function addClass(elem, className){return elem.classList.add(className);}
function removeClass(elem, className){return elem.classList.remove(className);}
function forEachNode(nodeList, func)
{
var i, n = nodeList.length;
for (i=0; i<n; i++)
{
func(nodeList[i], i, nodeList);
}
}
// 'boiler-plate' code above.
window.addEventListener('load', onDocLoaded, false);
function onDocLoaded()
{
myInit();
}
function myDodgyGetDay()
{
return 2;
}
function myInit()
{
var hours = ["Closed", "Closed", "9 AM to 9 PM", "9 AM to 5:30 PM","9 AM to 9 PM", "9 AM to 5:30 PM", "9 AM to 2:30 PM"]; //Sunday -> Saturday
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; // Variable to Convert getDay() to text
var dayOfWeek = myDodgyGetDay();
byId("open").innerHTML = "OPEN " + days[dayOfWeek] + " FROM " + days[dayOfWeek];
}
</script>
<style>
</style>
</head>
<body>
<p id="open"></p>
</body>
</html>