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 - 2 #34

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

[39장] DOM - 2 #34

juyeon-park opened this issue Aug 28, 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 28, 2023
@suehdn
Copy link
Collaborator

suehdn commented Aug 30, 2023

Q. 제가 만든 코드는 DOM을 한번만 변경하지만 필요 없는 컨테이너 <div>가 존재합니다. 이 컨테이너 요소를 추가하지 않으며 기존의 코드와 동일하게 DOM을 한번만 변경 시키는 방법으로 코드를 수정해주세요.

<!DOCTYPE html>
<body>
  <ul id = "JSStudy"></ul>
</body>
<script>
  const $JSStudy = document.getElementById('JSStudy');

  const $container = document.createElement('div');

  const people = ['건우','종현','영준','효리','승희','혜수','주연'];

  people.map(name => {
    const $li = document.createElement('li');
    const textNode = document.createTextNode(name);

    $li.appendChild(textNode);
    $container.appendChild($li);
  })

  $JSStudy.appendChild($container);
</script>
</html>
퀴즈 정답

const $container = document.createElement('div'); 부분을
const $container = document.createDocumentFragment(); 로 변경하게 되면 마지막에 containter를 JSStudy에 append하게 될 때 자신(DocumentFragment)은 제거되고 자식노드만 DOM에 추가된다.

@ghost
Copy link

ghost commented Aug 30, 2023

Q. 다음 O/X 문제에 답해주세요!

  1. 요소 노드는 2개의 상태, 초기 상태와 최신 상태를 관리해야 한다. 초기 상태는 DOM 프로퍼티가 관리하며, 요소 노드의 최신 상태는 어트리뷰트 노드가 관여한다.
  2. 자바스크립트 상에서 class 어트리뷰트에 대응하는 DOM 프로퍼티는 class가 아니라 className과 classList다. 자바스크립트에서 class는 예약어이기 때문이다.
퀴즈 정답
  1. X ( 요소 노드의 초기상태는 어트리뷰트 노드가 관리하고 최신 상태는 DOM 프로퍼티가 관리합니다. 참고로 최신 상태가 바뀌더라도 초기 상태는 변하지 않습니다. )

  2. O

@dudwns
Copy link
Member

dudwns commented Aug 30, 2023

Q. 다음 문제를 읽고 O / X로 답하시오.

  1. insertAdjacentHTML 메서드는 innterHTML 메서드와 달리 크로스 사이트 스크립팅 공격에 안전하다.
  2. Input 요소의 id 속성의 DOM 프로퍼티를 변경하면 어트리뷰트의 id도 변경된다.
  3. 어트리뷰트에 대응하는 DOM 프로퍼티 키는 대소문자를 구분하지 않는다.
퀴즈 정답
  1. X: innerHTML 프로퍼티와 마찬가지로 insertAdjacentHYML 메서드는 HTML 마크업 문자열을 파싱 하므로 크로스 사이트 스크립팅 공격에 취약하다는 점은 동일하다.

  2. O: 사용자 입력에 의한 상태 변화와 관계없는 어트리뷰트와 DOM 프로퍼티는 항상 동일한 값으로 연동한다.

  3. X: 어트리뷰트 이름은 대소문자를 구별하지 않지만 대응하는 프로퍼티 키는 카멜 케이스를 따른다.

@jonghyunlee95
Copy link
Collaborator

jonghyunlee95 commented Aug 30, 2023

Q. 다음 코드를 보고 li태그의 순서를 유추해보세요.

<!DOCTYPE html>
<html> 
  <body>
    <h1>Devcourse</h1>
    <ol id="devcourse-web">
      <li>backdung</li>
      <li>frong</li>
      <li>ai</li>
    </ol>
  </body>
  <script>
    const $devcourse = document.getElementById('devcourse-web');
    const [$back, ] = $devcourse.children;
    
    $devcourse.insertBefore($back, $devcourse.lastElementChild);
    
    $devcourse.removeChild($devcourse.lastElementChild);
  </script>
</html>
퀴즈 정답
1. frong
2. backdung

@juyeon-park
Copy link
Member Author

juyeon-park commented Aug 30, 2023

Q. 다음 코드의 실행 결과는?

<html>
  <body>
    <ul id="food">
      <li>짜장면</li>
      <li>짬뽕</li>
    </ul>
    <ul id="drink">
      <li>콜라</li>
      <li>맥주</li>
    </ul>
    <script>
      const $food = document.getElementById('food')
      const $li = document.createElement('li')
      $li.appendChild(document.createTextNode('탕수육'))
      $food.insertBefore($li, document.getElementById('drink').lastElementChild)
    </script>
  </body>
</html>
퀴즈 정답

DOMException 오류

image

insertBefore 메서드의 두번째 인수로 전달받은 노드는 반드시 insertBefore을 호출한 노드의 자식노드여야 합니다. 현재 코드의 두번째 인수 노드는 food노드의 자식이 아니므로 DOMException 에러가 발생합니다

@eeseung
Copy link
Collaborator

eeseung commented Aug 30, 2023

Q. 다음 코드에서 마지막 출력 결과로 frong을 얻기 위한 코드를 작성하고, 실행 결과를 예상해 주세요.

<!DOCTYPE html>
<html>
<head>
  <style>
    body { color: yellow; }
    .box { width: 100px; height: 200px; background-color: tomato; border: 1px solid black; }
    .box:before { content: 'frong' }
  </style>
</head>
<body>
  <div class="box">box</div>
  <script>
    const $box = document.querySelector('.box')
    const computedStyle = window.getComputedStyle($box)
    const computedStyleBefore = // (1) 의사 요소 :before 스타일을 취득하기 위한 코드

    console.log(computedStyle.width, computedStyle.height) // (2)
    console.log(computedStyle.display) // (3)
    console.log(computedStyleBefore.content) // frong
  </script>
</body>
</html>
퀴즈 정답
(1) window.getComputedStyle($box, ':before')
(2) 100px 200px
(3) block

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