我使用了在我的导师提供的网站上找到的代码,甚至让他来看看,但我们仍然不知道它不起作用。从那以后,我使用了简单的两行代码来输出日期/时间,但是我很想了解这一点并使其工作。我们将不胜感激-也是第一次使用该产品的用户,非常抱歉,因此,如果情况不符合要求,对不起
HTML代码
<p id="Date">Date and time will show here</p>
JS代码
function dateAndTime() {
var today = new Date();
var day = today.getDay();
var daylist = ["Sunday", "Monday", "Tuesday", "Wednesday ", "Thursday", "Friday", "Saturday"];
console.log("Today is : " + daylist[day] + ".");
var hour = today.getHours();
var minute = today.getMinutes();
var second = today.getSeconds();
console.log("Current Time : " + hour + " : " + minute + " : " + second);
// var date = new Date();
document.getElementById("Date").innerHTML = dateAndTime.toString();
}
我应该在屏幕上获得日期和时间,但是我什么也没得到,它甚至似乎都没有运行。
答案 0 :(得分:2)
@Gregory Sky请使用此代码。 将Java Script代码放在所有代码的底部。
您在Java脚本中具有访问元素。
<body>
<span id="Date"></span>
</body>
<script>
dateAndTime();
function dateAndTime() {
var today = new Date();
var day = today.getDay();
var daylist = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
console.log("Today is : " + daylist[day] + ".");
var hour = today.getHours();
var minute = today.getMinutes();
var second = today.getSeconds();
var dateTime= "Current Day:"+daylist[day] + " <br> Current Date And Time :"+hour + " : " + minute + " : " + second;
// var date = new Date();
document.getElementById("Date").innerHTML = dateTime;
}
</script>
答案 1 :(得分:0)
就像其他人说的那样,您并未运行该功能,因此它只是坐在那里而已。 (重构将使该功能更漂亮)
每个函数不过就是一段等待您调用它的代码。
检查此jsBin,我没有更改它,但是它可以运行它。 https://jsbin.com/nimubiqoma/1/edit?html,js,console,output
@Test (priority=1)
public void login()
{
System.out.println("login");
}
@Test(priority=2,dependsOnMethods={"login"})
public void method1()
{
System.out.println("method1");
}
@Test(priority=3,dependsOnMethods={"method1"})
public void method2()
{
System.out.println("method2");
}
@Test(priority=4,dependsOnMethods={"login"})
public void logout()
{
System.out.println("logout");
}
答案 2 :(得分:-1)
第一个问题是您没有在任何地方调用该函数,因此下面我使用了自调用函数,第二个问题是您正在将函数定义分配给innerHTML,相反,您应该像下面这样:
(function dateAndTime() {
var today = new Date();
var day = today.getDay();
var daylist = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
console.log("Today is : " + daylist[day] + ".");
var hour = today.getHours();
var minute = today.getMinutes();
var second = today.getSeconds();
console.log("Current Time : " + hour + " : " + minute + " : " + second);
// var date = new Date();
document.getElementById("Date").innerHTML = "Current Time : " + hour + " : " + minute + " : " + second;
})()
<p id="Date">Date and time will show here</p>