این محتوا تنها در این زبانها موجود است: عربي, English, Español, Français, Italiano, 日本語, 한국어, Русский, Українська, 简体中文. لطفاً به ما
Tag in comment
اهمیت: 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
.
What’s going on step by step:
- The content of
<body>
is replaced with the comment. The comment is<!--BODY-->
, becausebody.tagName == "BODY"
. As we remember,tagName
is always uppercase in HTML. - The comment is now the only child node, so we get it in
body.firstChild
. - The
data
property of the comment is its contents (inside<!--...-->
):"BODY"
.