[HTML/CSS] queryselector, addEventListener, target, classList 그리고 textContent

박지헌·2021년 9월 7일
0
const calculator = document.querySelector('.calculator'); // calculator 엘리먼트와, 그 자식 엘리먼트의 정보를 모두 담고 있습니다.
const buttons = calculator.querySelector('.calculator__buttons'); // calculator__keys 엘리먼트와, 그 자식 엘리먼트의 정보를 모두 담고 있습니다.

const firstOperend = document.querySelector('.calculator__operend--left'); // calculator__operend--left 엘리먼트와, 그 자식 엘리먼트의 정보를 모두 담고 있습니다.
const operator = document.querySelector('.calculator__operator'); // calculator__operator 엘리먼트와, 그 자식 엘리먼트의 정보를 모두 담고 있습니다.
const secondOperend = document.querySelector('.calculator__operend--right'); // calculator__operend--right 엘리먼트와, 그 자식 엘리먼트의 정보를 모두 담고 있습니다.
const calculatedResult = document.querySelector('.calculator__result'); // calculator__result 엘리먼트와, 그 자식 엘리먼트의 정보를 모두 담고 있습니다.

buttons.addEventListener('click', function (event) {
  // 버튼을 눌렀을 때 작동하는 함수입니다.
  const target = event.target; // 클릭된 HTML 엘리먼트의 정보가 저장되어 있습니다.
  const action = target.classList[0]; // 클릭된 HTML 엘리먼트에 클레스 정보를 가져옵니다.
  const buttonContent = target.textContent; // 클릭된 HTML 엘리먼트의 텍스트 정보를 가져옵니다.
  // ! 위 코드(Line 19 - 21)는 수정하지 마세요.

  if (target.matches('button')) {
    if (action === 'number') {
      if (numberValue == false) {                 //false에 의해 첫번째 칸에 아래 버튼의 content를 할당
        firstOperend.textContent = buttonContent  //버튼의 content를 첫번째칸에 할당한다.
        numberValue = true                        //두번째 칸에 새 값을 할당하기 위해 true로 바꾼다.

      } else {                                    //numberValue가 true일시 두번째 칸에 버튼의 content를 할당한다.
        secondOperend.textContent = buttonContent
        numberValue = false                       //다시 버튼을 누를시 첫번재 칸으로 할당되도록 false로 되돌린다.
      }

    }
profile
나는야 박지헌

0개의 댓글