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

home2 게시판 JavaScript, TS 게시판 탭기능 만들며 배우는 for 반복문 질문

탭기능 만들며 배우는 for 반복문 질문

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

    전태수
    참가자
    jQuery 방식 말고, 바닐라 스크립트로 구현하고 싶어서 이렇게 해봤는데,
    classList.remove 가 먹히질 않고 있는데, 어떤 문제가 있을까요?
    
    
      for(let i = 0; i < 3; i++){
                 document.querySelectorAll('.tab-button')[i].addEventListener('click', function(){
                    document.querySelectorAll('.tab-button')[i].classList.remove('orange');
                    document.querySelectorAll('.tab-button')[i].classList.add('orange');
                    
                    document.querySelectorAll('.tab-content')[i].classList.remove('show');
                    document.querySelectorAll('.tab-content')[i].classList.add('show');
                    console.log(i)  
                });
            }
    #110267

    codingapple
    키 마스터
    document.querySelectorAll('.tab-button')[0].classList.remove('orange');
    document.querySelectorAll('.tab-button')[1].classList.remove('orange');
    document.querySelectorAll('.tab-button')[2].classList.remove('orange');
    라고 쓰면 orange 다 없어질듯요 
    
    
    #110326

    전태수
    참가자
        for(let i = 0; i < 3; i++){
             document.querySelectorAll('.tab-button')[i].addEventListener('click', function(){
                // document.querySelectorAll('.tab-button')[i].classList.remove('orange');
                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')[i].classList.remove('show');
                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');
                console.log(i)  
            });
        }
    
    알려주신대로 이렇게 하니까 잘 되긴 하는데, document.querySelectorAll('.tab-button')[i].classList.remove('orange');
    이 방법으로는 전체 reomve가 힘들까요?
    #110346

    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 호 / 개인정보관리자 : 박종흠