Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[39장] DOM - 1 #32

Open
juyeon-park opened this issue Aug 23, 2023 · 6 comments
Open

[39장] DOM - 1 #32

juyeon-park opened this issue Aug 23, 2023 · 6 comments
Assignees
Labels

Comments

@juyeon-park
Copy link
Member

데브코스 4기 프롱이들 모던 JS Deep Dive 책 뿌수기😎

아래 템플릿을 복사해서 1개이상 퀴즈를 만들어주세요. 객관식, 주관식, 단답형 모두 상관없습니다!

Q. 퀴즈 내용
1.  1번
2.  2번
3.  3번

<details>
<summary>퀴즈 정답</summary>
<div markdown="1">    
정답 설명
</div>
</details>
@juyeon-park juyeon-park self-assigned this Aug 23, 2023
@suehdn
Copy link
Collaborator

suehdn commented Aug 28, 2023

Q. O/X 퀴즈입니다.

  1. 모든 노드 객체는 이벤트를 발생 시킬 수 있다.
  2. HTML 문서 내에서 id 값은 유일해야 하며 만약 중복된 id값을 갖는 HTML 요소가 여러 개 존재한다면 오류가 발생한다.
  3. id 어트리뷰트와 class 어트리뷰트는 공백 문자로 구분해 여러 개의 값을 가질 수 있다.
퀴즈 정답 1. 모든 노드는 EventTarget 인터페이스를 상속받는다.
2. 어떠한 에러도 발생하지 않는다. 만약에 원하는 id를 갖는 노드를 찾고싶을 때 getElementById 메서드를 사용한다면 전달된 id값을 갖는 첫 번째 요소 노드만 반환한다.
3. class는 여러 개의 값을 가질 수 있지만 id는 가능하지 않다.

@ghost
Copy link

ghost commented Aug 28, 2023

Q. 다음 코드에서 파란색이 되는 li 요소의 과일 이름을 모두 말해주세요!

<!DOCTYPE html>
<html>
  <head>
    <style>
      .red { color: red; }
      .blue { color: blue; }
    </style>
  </head>
  <body>
    <ul id="fruits">
      <li class="red">Apple</li>
      <li class="red">Banana</li>
      <li class="red">Orange</li>
    </ul>
    <script>
      const $elems = document.getElementsByClassName('red');
      for(let i = 0; i< $elems.length; i++) {
        $elems[i].className = 'blue';
      }
    </script>
  </body>
</html>
퀴즈 정답

image

getElementsByClassName 메서드가 반환하는 HTMLCollection 객체는 살아있는 객체이므로 반복문을 도는 와중 class를 변경할 시 실시간으로 HTMLCollection 객체도 바뀌게 되어 예상치 못한 동작이 일어난다.

위 같은 경우 첫번째 반복에서 Apple의 클래스가 blue 로 바뀌면 $elems의 첫번째(인덱스 0) 요소는 Banana가 된다. 그리고 for문의 i는 1이 되어 있을 것이고 이때 인덱스 1 요소는 Orange가 된다. 따라서 Orange 'blue'가 되고 반복문은 종료된다.

@jonghyunlee95
Copy link
Collaborator

jonghyunlee95 commented Aug 28, 2023

Q. 다음 코드의 의도는 frong class를 가진 li는 초록색, backdung class를 가진 li는 주황색으로 변경하는 것 입니다.
하지만 의도와는 다르게 frong class를 가진 li는 초록색으로 잘 변경되지만 backdung class를 가진 li는 주황색으로 변경되지않고
여전히 파란색입니다. 해결방법을 간단하게 설명해주세요.(해결방법은 여러가지 입니다!)

<!DOCTYPE html> 
<head>
  <style>
    .devcourse { color: blue; }
    .frong { color: green; }
    .backdung { color: orange; }
  </style>
</head>
<html>
  <body>
    <ul>
      <li class="devcourse">프롱이</li>
      <li class="devcourse">백둥이</li>
    </ul>
    <script>
      const $el = document.getElementsByClassName('devcourse');

      for (let i = 0; i < $el.length; i++) {
        if (i === 0) $el[i].className = 'frong';
        else $el[i].className = 'backdung';
      }
    </script>
  </body>
</html>
퀴즈 정답
해결방법
<script>
  // 스프레드 연산자를 이용한 방법
  const $el = document.getElementsByClassName('devcourse')
  const $elArr = [...$el];

  for (let i = 0; i < $elArr.length; i++) {
    if (i === 0) $elArr[i].className = 'frong';
    else $elArr[i].className = 'backdung';
  }

// +while문을 이용하는 방법, for문을 역방향으로 순회하는 방법도 있습니다.
</script>

@dudwns
Copy link
Member

dudwns commented Aug 28, 2023

Q. 퀴즈 내용

  1. 다음 중 에러가 발생하는 부분을 올바르게 고치고 그 이유를 서술하시오.
<!DOCTYPE html>
<html>
  <head>
    <title>Dom 예제</title>
  </head>
  <body>
    <ul>
      <li class="language java">Java</li>
      <li class="language javascript">JavaScript</li>
      <li class="language kotlin">Kotlin</li>
    </ul>
    <script>
      const $languageLi = document.getElementsByClassName("language javascript");
      $languageLi.forEach((elem) => {
        elem.style.color = "yellow";
      });
    </script>
  </body>
</html>
퀴즈 정답
getElementByClassName 메서드는 여러 개의 요소 노드 객체를 갖는 DOM 컬렉션 객체인 HTMLCollection 객체를 반환한다. HTMLCollection 객체는 forEach 메서드를 지원하지 않기 때문에 배열로 변환해야 한다.
[...$languageLi].forEach

@hyoribogo
Copy link
Member

Q. 다음 나열된 인터페이스들을 상속 순서에 맞게 답을 써주세요.
HTMLElement / EventTarget / Node / Object / HTMLInputElement / Element

(화살표 방향대로 상속됩니다)
(a) > (b) > (c) > (d) > (e) > (f) > input

퀴즈 정답

Object > EventTarget > Node > Element > HTMLElement > HTMLInputElement > input

@juyeon-park
Copy link
Member Author

juyeon-park commented Aug 28, 2023

Q. 자식 노드를 탐색하기 위한 노드 탐색 프로퍼티들입니다. 그에 맞는 특징들을 순서대로 나열해주세요.

Node.prototype.childNodes
Element.prototype.children
Node.prototype.firstChild
Element.prototype.firstElementChild
  1. 첫번째 자식 요소 노드를 반환한다. 이 노드는 요소 노드만 반환한다.
  2. 자식 노드를 모두 탐색하여 요소 노드뿐만 아니라 텍스트 노드도 포함하여 반환한다.
  3. 첫 번째 자식 노드를 반환한다. 이 자식 노드는 텍스트 노드 or 요소 노드다.
  4. 자식 노드 중에서 요소 노드만 모두 탐색하여 HTMLCollection에 담아 반환한다.
퀴즈 정답
2-4-3-1

@eeseung eeseung mentioned this issue Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants