按下按钮后,整个页面被简单地替换为白色
function print (text) {
document.write();
}
var Name, Age;
var y = new Date;
function userValue (name, age, dateOfBirth) {
this.name = name;
this.age = age;
this.dateOfBirth = y.getFullYear() - age;
}
function conf() {
document.getElementById ('inp1').value = Name;
document.getElementById ('inp2').value = Age;
var user1 = new userValue (Name, Age);
print ('Your name is ' + user1.Name);
print ('Your age is ' + user1.Age);
print ('You were born in' + user1.dateOfBirth);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<input type="text" placeholder="Input your name" id="inp1">
<input type="text" placeholder="Input your age" id="inp2">
<button onclick="conf(this)">Ok</button>
<script src="script.js"></script>
</body>
</html>
都是因为该函数
print
包含一个document.write()
不接受任何东西的方法。修复document.write(text)
它,一切都会好起来的。一切都会出现在页面上,但你传递的变量将是undefined
NaN我要补充一点:由于 js 区分大小写,因此您传递给函数的数据
print
将作为undefined
. 看一下 user1 对象中的内容就足够了,它包含所有带有小写字母的属性,这意味着这将是正确的: