=============================코드=============================
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
let student; // 전역변수
// 학생의 정보를 입력하는 함수
function insertStudent() {
student = {
hakbun : prompt("학생 학번 입력"),
name : prompt("학생 이름 입력"),
major : prompt("학생 학과 입력"),
phone : prompt("학생 연락처 입력"),
/*
studentInfo : function() {
let str = "학생 학번 : " + this.hakbun + "<br/>" +
"학생 이름 : " + this.name + "<br/>" +
"학생 학과 : " + this.major + "<br/>" +
"학생 연락처 : " + this.phone
return str;
} */
// 화살표 함수로 선언
studentInfo : () => {
let str = "<ul><li>학생 학번 : " + student.hakbun + "</li>" +
"<li>학생 이름 : " + student.name + "</li>" +
"<li>학생 학과 : " + student.major + "</li>" +
"<li>학생 연락처 : " + student.phone + "</li></ul>";
return str;
}
}
} // insertStudent() 함수 end
// 학생의 정보를 출력하는 함수
function printStudent() {
if(student != null){
// innerHTML : 요소 안에 있는 태그와 내용을 함께 가져오는 속성
document.getElementById("output").innerHTML
// 현재 요소의 id요소(div 태그에 있음)를 가져와라!
= student.studentInfo();
}else {
alert("학생의 정보를 입력해주세요!");
}
} // printStudent() 함수 end
</script>
</head>
<body>
<h2>출력창</h2>
<div id = "output"></div>
<br/>
<input type = "button" value = "입력" onclick = "insertStudent()">
<input type = "button" value = "출력" onclick = "printStudent()">
</body>
</html>
