你的生日是星期几?HTML+JavaScript帮你列出来
要创建一个简单的网页,用户可以输入他们的生日日期,然后网页会显示这个日期是星期几,你可以使用以下HTML和JavaScript代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>What Day is Your Birthday?</title>
<script>
function calculateDayOfWeek() {
var birthday = new Date(document.getElementById('birthday').value);
var dayOfWeek = birthday.getDay();
var daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
document.getElementById('result').innerText = 'Your birthday is a ' + daysOfWeek[dayOfWeek];
}
</script>
</head>
<body>
<label for="birthday">Enter your birthday (YYYY-MM-DD):</label>
<input type="date" id="birthday" name="birthday">
<button onclick="calculateDayOfWeek()">Submit</button>
<p id="result"></p>
</body>
</html>
用户在输入框中输入日期,点击提交按钮后,JavaScript函数calculateDayOfWeek
会被调用,计算出该日期是星期几,并将结果显示在页面底部的<p>
标签中。
评论已关闭