欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 旅游 > 重学前端:一文理解 JS Document 对象中的属性和方法

重学前端:一文理解 JS Document 对象中的属性和方法

2025/2/6 19:36:54 来源:https://blog.csdn.net/qq_24956515/article/details/144579381  浏览:    关键词:重学前端:一文理解 JS Document 对象中的属性和方法

一. JS DOM(文档对象模型)

文档对象模型(Document Object Model,简称 DOM),是一种与平台和语言无关的模型,用来表示 HTML 或 XML 文档。文档对象模型中定义了文档的逻辑结构,以及程序访问和操作文档的方式。

当网页加载时,浏览器就会自动创建当前页面的文档对象模型(DOM)。在 DOM 中,文档的所有部分(例如元素、属性、文本等)都会被组织成一个逻辑树结构(类似于族谱),树中每一个分支的终点称为一个节点,每个节点都是一个对象,如下图所示:

fileOf7298.png

 

借助 DOM 您可以使用 JavaScript 来访问、修改、删除或添加 HTML 文档中的任何内容。

二. Document 对象

当浏览器加载一个 HTML 文档时,会创建一个 Document 对象,Document 对象是 DOM 树中所有节点的根节点。通过 Document 对象我们可以访问 HTML 文档中的所有元素。

提示:Document 对象是 Window 对象的一部分,所以您可以通过 window.document 来访问 Document 对象。

三. Document 对象中的属性

下面总结列举了 Document 对象中提供的属性及其说明:

  1. document.activeElement 返回当前获取焦点的元素

  2. document.anchors 返回对文档中所有 Anchor 对象的引用

  3. document.applets 返回对文档中所有 Applet 对象的引用。注意: HTML5 已不支持 元素

  4. document.baseURI 返回文档的基础 URI

  5. document.body 返回文档的 body 元素

  6. document.cookie 设置或返回与当前文档有关的所有 cookie

  7. document.doctype 返回与文档相关的文档类型声明 (DTD)

  8. document.documentElement 返回文档的根节点

  9. document.documentMode 返回浏览器渲染文档的模式

  10. document.documentURI 设置或返回文档的位置

  11. document.domain 返回当前文档的域名

  12. document.domConfig 已废弃,返回 normalizeDocument() 被调用时所使用的配置

  13. document.embeds 返回文档中所有嵌入内容(embed)的集合

  14. document.forms 返回文档中所有 Form 对象的引用

  15. document.images 返回文档中所有 Image 对象的引用

  16. document.implementation 返回处理该文档的 DOMImplementation 对象

  17. document.inputEncoding 返回文档的编码方式

  18. document.lastModified 返回文档的最后修改日期

  19. document.links 返回对文档中所有 Area 和 Link 对象的引用

  20. document.readyState 返回文档状态(载入中)

  21. document.referrer 返回载入当前文档的 URL

  22. document.scripts 返回页面中所有脚本的集合

  23. document.strictErrorChecking 设置或返回是否强制进行错误检查

  24. document.title 返回当前文档的标题

  25. document.URL 返回文档的完整 URL

四. Document 对象中的方法

下面总结列举了 Document 对象中提供的方法及其说明:

  1. document.addEventListener() 向文档中添加事件

  2. document.adoptNode(node) 从另外一个文档返回 adapded 节点到当前文档

  3. document.close() 关闭使用 document.open() 方法打开的输出流,并显示选定的数据

  4. document.createAttribute() 为指定标签添加一个属性节点

  5. document.createComment() 创建一个注释节点

  6. document.createDocumentFragment() 创建空的 DocumentFragment 对象,并返回此对象

  7. document.createElement() 创建一个元素节点

  8. document.createTextNode() 创建一个文本节点

  9. document.getElementsByClassName() 返回文档中所有具有指定类名的元素集合

  10. document.getElementById() 返回文档中具有指定 id 属性的元素

  11. document.getElementsByName() 返回具有指定 name 属性的对象集合

  12. document.getElementsByTagName() 返回具有指定标签名的对象集合

  13. document.importNode() 把一个节点从另一个文档复制到该文档以便应用

  14. document.normalize() 删除空文本节点,并合并相邻的文本节点

  15. document.normalizeDocument() 删除空文本节点,并合并相邻的节点

  16. document.open() 打开一个流,以收集来自 document.write() 或 document.writeln()

  17. document.querySelector() 返回文档中具有指定 CSS 选择器的第一个元素

  18. document.querySelectorAll() 返回文档中具有指定 CSS 选择器的所有元素

  19. document.removeEventListener() 移除文档中的事件句柄

  20. document.renameNode() 重命名元素或者属性节点

  21. document.write() 向文档中写入某些内容

  22. document.writeln() 等同于 write() 方法,不同的是 writeln() 方法会在末尾输出一个换行符

示例代码如下:

document.addEventListener("click", function(){document.body.innerHTML = document.activeElement;var box = document.createElement('div');document.body.appendChild(box);var att = document.createAttribute('id');att.value = "myDiv";document.getElementsByTagName('div')[0].setAttributeNode(att);document.getElementById("myDiv").innerHTML = Math.random();var btn = document.createElement("button");var t = document.createTextNode("按钮");btn.appendChild(t);document.body.appendChild(btn);var att = document.createAttribute('onclick');att.value = "myfunction()";document.getElementsByTagName('button')[0].setAttributeNode(att);
});
function myfunction(){alert(document.title);
}

运行上面的代码,点击页面的空白区域,即可输出如下图所示的内容:

fileOf7298.png

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com