gpt4 book ai didi

javascript - Document Html 在 javascript 中真正的文档是什么

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:05 25 4
gpt4 key购买 nike

我有代码html代码:

<!DOCTYPE html>
<html><head></head><body><script src="index.js"></script></body></html>

和 index.js 中的 javascript 代码:

console.log(document)// HTMLDocument
console.log(document.childNodes[0])//Object DocumentType

但是当我在 html 代码中删除 Doctype 并运行 console.log(document.childNodes[0]) 我得到 HTML 元素

这是什么意思?为什么文档有子节点?javascript 中的真正文档是什么?

最佳答案

document.childNodes[0]是页面的第一个元素。如果有 <!DOCTYPE html> , 那么这是第一个元素,因此它返回 DocumentType。

如果DOCTYPE不存在,则第一个元素是 <html>标签。因此 document.childNodes[0]是页面的元素,因为所有其他标签都包含在 html 中标签(但 DOCTYPE 标签中不包含任何内容)

关于javascript - Document Html 在 javascript 中真正的文档是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40354405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com