Notice
Recent Posts
Recent Comments
Link
«   2025/03   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter03 : DOM생성 본문

WEB.JS/08.DOM_JS

chapter03 : DOM생성

GAWON 2023. 5. 23. 09:16
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
// 해당 배열 내의 데이터를 이용하여 리스트 만들기 (요소 생성 함수 사용)

onload = function() {

```
  var fruits = ["apple", "banana", "orange", "kiwi", "grape"];

  for (var i = 0; i < fruits.length; i++) {
     // 1. li 태그 작성
     var list = document.createElement("li");   // <li></li>

     // 2. 배열에 저장된 내용 가져오기
     var fName = document.createTextNode(fruits[i]);      // apple

     // 3. li 태그에 텍스트 노드 추가
     list.appendChild(fName);      // <li>apple</li>

     // 4. ul 태그에 li 태그 추가
     document.getElementById("list").appendChild(list);
  }

```

}
</script>
</head>
<body>
<ul id="list"></ul>
</body>
</html>

'WEB.JS > 08.DOM_JS' 카테고리의 다른 글

chapter01 : DOM개요  (0) 2023.05.23
chapter02 : DOM생성  (0) 2023.05.23
chapter04 : DOM생성  (0) 2023.05.23
chapter05 : DOM삭제  (0) 2023.05.23
chapter06 : DOM_getElementById  (0) 2023.05.23