• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 JavaScript, TS 게시판 탭 버튼 기능2 for반복문 질문 드립니다

탭 버튼 기능2 for반복문 질문 드립니다

4 글 보임 - 1 에서 4 까지 (총 4 중에서)
  • 글쓴이
  • #32742

    코딩이이
    참가자

    선샌님.... 제이쿼리 반복문은 직접 쓸 수 있을 것 같은데

    순수 자바스크립트로 작성한 것도 for문으로 바꾸고 싶은데 .....

    열심히 생각해봤지만 ..... 머리가 꼬이는 기분입니다 ...

    검색해봐도 글들이 아직 배우지 않은 영역으로 작성 되어 있는 코드들로 있어서 이해하기 어렵네요

    document.querySelectorAll(".tab-button")[0].addEventListener("click", function () {
            document.querySelectorAll(".tab-button")[0].classList.remove("orange");
            document.querySelectorAll(".tab-button")[1].classList.remove("orange");
            document.querySelectorAll(".tab-button")[2].classList.remove("orange");
            document.querySelectorAll(".tab-button")[0].classList.add("orange");

            document.querySelectorAll(".tab-content")[0].classList.remove("show");
            document.querySelectorAll(".tab-content")[1].classList.remove("show");
            document.querySelectorAll(".tab-content")[2].classList.remove("show");
            document.querySelectorAll(".tab-content")[0].classList.add("show");
          });

    document.querySelectorAll(".tab-button")[1].addEventListener("click", function () {
            document.querySelectorAll(".tab-button")[0].classList.remove("orange");
            document.querySelectorAll(".tab-button")[1].classList.remove("orange");
            document.querySelectorAll(".tab-button")[2].classList.remove("orange");
            document.querySelectorAll(".tab-button")[1].classList.add("orange");
            document.querySelectorAll(".tab-content")[0].classList.remove("show");
            document.querySelectorAll(".tab-content")[1].classList.remove("show");
            document.querySelectorAll(".tab-content")[2].classList.remove("show");
            document.querySelectorAll(".tab-content")[1].classList.add("show");
          });
          document.querySelectorAll(".tab-button")[2].addEventListener("click", function () {
            document.querySelectorAll(".tab-button")[0].classList.remove("orange");
            document.querySelectorAll(".tab-button")[1].classList.remove("orange");
            document.querySelectorAll(".tab-button")[2].classList.remove("orange");
            document.querySelectorAll(".tab-button")[2].classList.add("orange");
            document.querySelectorAll(".tab-content")[0].classList.remove("show");
            document.querySelectorAll(".tab-content")[1].classList.remove("show");
            document.querySelectorAll(".tab-content")[2].classList.remove("show");
            document.querySelectorAll(".tab-content")[2].classList.add("show");
          });

    요렇게 버튼 3개를 for문으로 바꿀라면 어떻게 해야할까요 ...........??

    #32751

    codingapple
    키 마스터

    for (){

      위에있는 코드10줄

    }

    이게 끝입니다 0이라는 숫자 대신 i 넣으면 되겠군요 

    #32762

    코딩이이
    참가자

          for (let i = 0; i < 3; i++) {
            document.querySelectorAll(".tab-button")[i].addEventListener("click", function () {
              document.querySelectorAll(".tab-button")[0].classList.remove("orange");
              document.querySelectorAll(".tab-button")[1].classList.remove("orange");
              document.querySelectorAll(".tab-button")[2].classList.remove("orange");
              document.querySelectorAll(".tab-button")[i].classList.add("orange");

              document.querySelectorAll(".tab-content")[0].classList.remove("show");
              document.querySelectorAll(".tab-content")[1].classList.remove("show");
              document.querySelectorAll(".tab-content")[2].classList.remove("show");
              document.querySelectorAll(".tab-content")[i].classList.add("show");
            });
          }

    이게 맞을까요?? 이렇게 하니까 이제 작동이 되네요 !! 

    #32783

    codingapple
    키 마스터

    의도대로 잘되면 정답입니다 

4 글 보임 - 1 에서 4 까지 (총 4 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠