我的下面的JavaScript代码有什么问题? [英] What is wrong with my below code for javascript?
本文介绍了我的下面的JavaScript代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想按照以下格式显示日期和日期,但对我无效。显示Uncaught TypeError:无法设置属性'innerHTML'为null错误。任何人都可以帮我解决这个问题吗?
输出: -
:星期三
现在时间是:8 PM:25:20
我的HTML代码: - * *
<!DOCTYPE html>
< html>
< head>
< meta charset = utf-8 />
< title> JS Excercise< / title>
< script src =Js\DateTimeExcercise.js>
< / script>
< / head>
< body>
今天是:< span id =day>< / span>
< br />< br />
当前时间为:< span id =time>< / span>
< / body>
< / html>
**我的Java脚本代码: - **
var day,time,Sec,Min,Hr,AMPM;
AMPM =AM;
day = new Date()。getDay();
Sec = new Date()。getSeconds();
Min = new Date()。getMinutes();
Hr = new Date()。getHours();
if(hr> 12)
{
Hr = Hr - 12;
AMPM =PM;
}
switch(day)
{
case 0:
txt =Sunday;
休息;
案例1:
txt =星期一;
休息;
案例2:
txt =Tueday;
休息;
案例3:
txt =星期三;
休息;
案例4:
txt =Thrusday;
休息;
案例5:
txt =星期五;
休息;
案例6:
txt =星期六;
休息;
}
document.getElementById(day)。innerHTML = txt;
document.getElementById(time)。innerHTML = Hr ++ AMPM ++:+ Min +:+ Sec;
解决方案
尝试在底部调用脚本
<!DOCTYPE html>
< html>
< head>
< meta charset = utf-8 />
< title> JS Excercise< / title>
< / head>
< body>
今天是:< span id =day>< / span>
< br />< br />
当前时间为:< span id =time>< / span>
< / body>
< script src =Js\DateTimeExcercise.js>
< / script>
< / html>
I want to display the day and date in following format but it doesn't work for me. Error showing "Uncaught TypeError: Cannot set property 'innerHTML' of null". Can anyone get me out of this?
Output:-
Today is : Wednesday
Current time is : 8 PM :25:20
** My HTML Code:-**
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Excercise</title>
<script src="Js\DateTimeExcercise.js">
</script>
</head>
<body>
Today is : <span id="day"></span>
<br/><br/>
Current time is : <span id="time"></span>
</body>
</html>
** My Java Script Code:-**
var day, time, Sec, Min, Hr, AMPM;
AMPM = "AM";
day = new Date().getDay();
Sec = new Date().getSeconds();
Min = new Date().getMinutes();
Hr = new Date().getHours();
if (Hr>12)
{
Hr = Hr - 12;
AMPM = "PM";
}
switch(day)
{
case 0:
txt = "Sunday";
break;
case 1:
txt = "Monday";
break;
case 2:
txt = "Tueday";
break;
case 3:
txt = "Wednesday";
break;
case 4:
txt = "Thrusday";
break;
case 5:
txt = "Friday";
break;
case 6:
txt = "Saturday";
break;
}
document.getElementById("day").innerHTML = txt;
document.getElementById("time").innerHTML = Hr + " "+ AMPM + " "+ ":" + Min + ":" + Sec;
解决方案
Try calling the script at the bottom of the html.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Excercise</title>
</head>
<body>
Today is : <span id="day"></span>
<br/><br/>
Current time is : <span id="time"></span>
</body>
<script src="Js\DateTimeExcercise.js">
</script>
</html>
这篇关于我的下面的JavaScript代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文