kembali ke pelajaran
Materi ini hanya tersedia dalam bahasa berikut: عربي, English, Español, Français, Italiano, 日本語, 한국어, Русский, Українська, 简体中文. Tolong, bantu kami menerjemahkan ke dalam Indonesia.

Tag in comment

pentingnya: 3

What does this code show?

<script>
  let body = document.body;

  body.innerHTML = "<!--" + body.tagName + "-->";

  alert( body.firstChild.data ); // what's here?
</script>

The answer: BODY.

<script>
  let body = document.body;

  body.innerHTML = "<!--" + body.tagName + "-->";

  alert( body.firstChild.data ); // BODY
</script>

What’s going on step by step:

  1. The content of <body> is replaced with the comment. The comment is <!--BODY-->, because body.tagName == "BODY". As we remember, tagName is always uppercase in HTML.
  2. The comment is now the only child node, so we get it in body.firstChild.
  3. The data property of the comment is its contents (inside <!--...-->): "BODY".