我正在尝试使用JS根据当前日期自动计算人员年龄,然后使用内部HTML将结果插入到跨度中,但是我一直得到NAN作为结果。知道为什么吗?
我尝试将var x代码放在不同的位置,还尝试插入getAge
作为innerHTML
,但这只是插入了整个JS。
var x = getAge();
document.getElementById("age-result").innerHTML = x;
function getAge(dateString) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
console.log('age: ' + getAge("1969/01/05"));
<span id="age-result"></span>
我在做什么错了?
console.log
显示正确的结果。
答案 0 :(得分:1)
MSS = MTU - (20(IP header) + len(IP Options)) - (20(TCP Header) + len(TCP Options))
var x = getAge("1969/01/05");
document.getElementById("age-result").innerHTML = x;
function getAge(dateString) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
您忘记将参数传递给<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<span id="age-result"></span>
</body>
</html>
函数。
尝试一下:
getAge
var x = getAge("1969/01/05");
document.getElementById("age-result").innerHTML = x;
function getAge(dateString) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}