html通过js两种方式分别定义一个对象,存放自己的学号、姓名、性别、年龄、专业、班级,并在浏览器中输出对象的内容(学号、姓名、性别、年龄、专业、班级)
时间: 2023-07-03 13:11:02 浏览: 61
第一种方式:
```html
<!DOCTYPE html>
<html>
<head>
<title>定义对象并输出</title>
</head>
<body>
<script>
// 定义对象
var student = {
"学号": "20210001",
"姓名": "张三",
"性别": "男",
"年龄": 20,
"专业": "计算机科学与技术",
"班级": "计科2001"
};
// 输出对象内容
document.write("学号:" + student.学号 + "<br>");
document.write("姓名:" + student.姓名 + "<br>");
document.write("性别:" + student.性别 + "<br>");
document.write("年龄:" + student.年龄 + "<br>");
document.write("专业:" + student.专业 + "<br>");
document.write("班级:" + student.班级 + "<br>");
</script>
</body>
</html>
```
第二种方式:
```html
<!DOCTYPE html>
<html>
<head>
<title>定义对象并输出</title>
</head>
<body>
<script>
// 定义对象
var student = new Object();
student.学号 = "20210001";
student.姓名 = "张三";
student.性别 = "男";
student.年龄 = 20;
student.专业 = "计算机科学与技术";
student.班级 = "计科2001";
// 输出对象内容
document.write("学号:" + student.学号 + "<br>");
document.write("姓名:" + student.姓名 + "<br>");
document.write("性别:" + student.性别 + "<br>");
document.write("年龄:" + student.年龄 + "<br>");
document.write("专业:" + student.专业 + "<br>");
document.write("班级:" + student.班级 + "<br>");
</script>
</body>
</html>
```
阅读全文