<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>텍스트 필드에 입력한 값을 화면에 표시하기 </title>
<style>
* {
box-sizing:border-box;
}
#container {
width:500px;
margin:20px auto;
padding:20px;
}
input[type="text"] {
width:370px;
float:left;
height:30px;
padding-left:30px;
}
button {
width:90px;
height:30px;
float:right;
background:#222;
color:#fff;
border:none;
}
hr {clear:both; display:none};
ul {list-style-type:none; padding-top:50px;}
li {
line-height:2.5;
list-style-type:none;
}
li:hover {cursor:pointer;} /*마우스 커서 모양 -손가락 모양*/
</style>
</head>
<body>
<div id="container">
<h1>Web programming</h1>
<p>공부할 주제를 기록해 보세요</p>
<form action="">
<input type="text" id="subject" autofocus>
<button onclick="newRegister(); return false;">추가</button>
</form>
<hr>
<ul id="itemList"></ul>
</div>
<script>
function newRegister() {
var newItem = document.createElement("li"); //새로운 li 요소 노드를 추가
var subject = document.querySelector("#subject"); //폼의 텍스트 필드를 가져옴
var newText = document.createTextNode(subject.value); //필드값을 텍스트 노드로 추가
newItem.appendChild(newText); //텍스트 노드를 자식 노드로 연결
var itemList = document.querySelector("#itemList"); //부모 노드를 가져옴
itemList.appendChild(newItem); //새로 만든 요소 노드를 부모 노드에 추가
subject.value=""; //다음 입력을 위해 텍스트 필드 비우기
}
</script>
</body>
</html>
'JavaScript' 카테고리의 다른 글
17장 마무리 문제 1 (0) | 2023.04.14 |
---|---|
17장 예제 - DOM에서 노드 삭제하기 / 실습 3 (2) | 2023.04.14 |
17장 예제 -DOM에서 노드 추가하기 (0) | 2023.04.14 |
17장 실습 (2) | 2023.04.14 |
17장 예제 - DOM에서 이벤트 처리하기 (2) (0) | 2023.04.14 |