Javascript [ QuerySelector - For / QuerySelectorAll - For ]

양혜정·2024년 4월 7일
0

javascript_web

목록 보기
19/81

QuerySelector - For

  • arr_total, arr_avg, arr_grade 배열 참고 확인
  • 하나의 값일 경우 for 문에 item 로 표현
let html = `<table>
				<tr>
					<th>학번</th>
					<th>성명</th>
					<th>국어</th>
                   	<th>영어</th>
                   	<th>수학</th>
                   	<th>총점</th>
                   	<th>평균</th>
                   	<th>등급</th>
                </tr>`;
배열명.forEach((item, index) => {
	html += `<tr>
				<td>${item.hakbun}</td>
                <td>${item.name}</td>
                <td>${item.kor}</td>
                <td>${item.eng}</td>
                <td>${item.math}</td>
                <td>${arr_total[index]}</td>
                <td>${arr_avg[index]}</td>
                <td>${arr_grade[index]}</td>
			</tr>`;
});

html += `</table>`;

document.querySelector("넣을위치").innerHTML = html;

QuerySelectorAll - For

  • Nodelist 일 경우 for 문에 elmt 로 표현
// === 합격자 명단 보이기 === //
const tr_list 
	= document.querySelectorAll("table#tbl > tbody > tr");
// tr_list 는 NodeList 이다. [tr,tr...]
let html = `<table>
              <thead>
              	<tr>
              		<th width="50%">합격자명</th>
              		<th>점수</th>
              	</tr>
              </thead>
              <tbody>`;
tr_list.forEach((elmt,index) => {
	if(index < tr_list.length-1 
       && Number(elmt.children[1].innerText) >= 80){
      	html += `<tr>
                	<td>${elmt.children[0].innerText}</td>
                   	<td>${elmt.children[1].innerText}</td>
				</tr>`;
    }
})	//  end of forEach------------
html += `</tbody></table>`;

document.querySelector("넣을장소").innerHTML = html;

.children

  • 엘리먼트(태그).children[숫자]
  • 해당 엘리먼트(태그)의 자식엘리먼트(태그) 숫자번째를 배열의 형태로 반환

참고

arr_total, arr_avg, arr_grade : https://velog.io/@jjoung-2j/Javascript-Map-%ED%95%99%EC%83%9D-%EC%A0%90%EC%88%98-%ED%91%9C%ED%98%84


정리

  • 09_javascriptStandardObject
    -> 01_Array_class -> 02_map_children.html, 02.css, 02.js

0개의 댓글

관련 채용 정보