text

유석현(SeokHyun Yu)·2022년 11월 16일
0

JavaScript

목록 보기
35/44
post-thumbnail
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <p id="target1"><span>Hello world</span></p>
    <!-- 여기에 공백 문자가 존재하므로 t2의 firstChild는 공백 문자 -->
    <p id="target2">
      <span>Hello world</span>
    </p>
    <script>
      const t1 = document.getElementById("target1").firstChild;
      const t2 = document.getElementById("target2").firstChild;

      console.log(t1.firstChild.nodeValue);
      try {
        console.log(t2.firstChild.nodeValue);
      } catch (e) {
        console.log(e);
      }
      console.log(t2.nextSibling.firstChild.nodeValue);
    </script>
  </body>
</html>

profile
Backend Engineer

0개의 댓글