[英]Beginner JS, function not working
So I'm new to JavaScript - like a few days in "new," and I don't understand why this function that I'm calling isn't working. 因此,我是JavaScript的新手,就像几天在“ new”中一样,而且我不明白为什么我要调用的函数无法正常工作。
I have this HTML page: 我有这个HTML页面:
<!doctype html>
<html>
<head>
<title>Document</title>
</head>
<body>
<h1>Practice</h1>
<button id="btn" onclick="printDate()">Print Date</button>
<p id="Day"></p>
<button onclick="printTime()"> Show Clock </btn>
</br>
<p id="Time"></p>
<script src="Clock.js"></script>
</body>
</html>
And clock.js has the following: Clock.js具有以下内容:
// Defines Function
function printDate() {
// Grabs values from "Date" object using various methods
var date = new Date();
var day = date.getDay();
var dateNum = date.getDate();
var month = date.getMonth();
var year = date.getYear();
// Corrects for JS year format
year += 1900
// Switch "day" value with string containing name of day
switch(day){
case 0: day = "Sunday"
break;
case 1: day = "Monday"
break;
case 2: day = "Tuesday"
break;
case 3: day = "Wednesday"
break;
case 4: day = "Thursday"
break;
case 5: day = "Friday"
break;
case 6: day = "Saturday"
break;
}
// Switch "month" value with string containing name of month
switch(month){
case 0: month = "January"
break;
case 1: month = "February"
break;
case 2: month = "March"
break;
case 3: month = "April"
break;
case 4: month = "May"
break;
case 5: month = "June"
break;
case 6: month = "July"
break;
case 7: month = "August"
break;
case 8: month = "September"
break;
case 9: month = "October"
break;
case 10: month = "November"
break;
case 11: month = "December"
break;
}
// Prints values into p tag ID'd with "Day"
document.getElementById("Day").innerHTML = "Today is: "+day+" the "+dateNum+" of "+month+", "+year
}
function printTime() {
document.getElementById("Time").innerHTML = "Current Time"
}
Sorry for the comments, they're for my own learning purposes. 对不起,您的评论是出于我自己的学习目的。
So to the issue - when I try to call printTime() in the HTML doc, it doesn't work, and doesn't replace the innerHTML with "Current Time" however, the other function works fine. 因此,问题-当我尝试在HTML文档中调用printTime()时,它不起作用,并且不将“ innerTime”替换为“ innerHTML”,但是其他功能也可以正常工作。 What am I doing wrong here?
我在这里做错了什么? Why does the first function work, but not the second?
为什么第一个功能起作用,而第二个功能却不起作用?
Any help would be greatly appreciated! 任何帮助将不胜感激!
You have an incorrect closing tag for that button. 您有该按钮的不正确的结束标记。 You have
</btn>
when it should be </button>
. </btn>
应该是</button>
。
off topic: just a little refactoring of your script 不在主题上:只需重构一下脚本
var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
function printDate() {
// Grabs values from "Date" object using various methods
var date = new Date();
var dateNum = date.getDate();
var day = weekdays[date.getDay()];
var month = months[date.getMonth()];
var year = date.getFullYear();
// Prints values into p tag ID'd with "Day"
document.getElementById("Day").innerHTML = "Today is: "+day+" the "+dateNum+" of "+month+", "+year;
}
function printTime() {
document.getElementById("Time").innerHTML = "Current Time"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.