DOM 基础

本文介绍了DOM(文档对象模型)的基本概念及其在JavaScript中如何用于操作HTML文档。从W3C发布DOM Level 1标准开始,浏览器实现了对DOM推荐标准的支持,减少了浏览器间的不兼容问题。DOM不仅适用于HTML,也适用于XML和XHTML文档。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

What is the DOM?

With JavaScript you can restructure an entire HTML document. You can add, remove, change, or reorder items on a page.

To change anything on a page, JavaScript needs access to all elements in the HTML document. This access, along with methods and properties to add, move, change, or remove HTML elements, is given through the Document Object Model (DOM).

In 1998, W3C published the Level 1 DOM specification. This specification allowed access to and manipulation of every single element in an HTML page.

All browsers have implemented this recommendation, and therefore, incompatibility problems in the DOM have almost disappeared.

The DOM can be used by JavaScript to read and change HTML, XHTML, and XML documents.

The DOM is separated into different parts (Core, XML, and HTML) and different levels (DOM Level 1/2/3):

Core DOM - defines a standard set of objects for any structured document XML DOM - defines a standard set of objects for XML documents HTML DOM - defines a standard set of objects for HTML documents

Everything in an HTML document is a node.

Nodes

According to the DOM, everything in an HTML document is a node.

The DOM says that:

The entire document is a document node Every HTML tag is an element node The texts contained in the HTML elements are text nodes Every HTML attribute is an attribute node Comments are comment nodes

 Node Hierarchy

Nodes have a hierarchical relationship to each other.

All nodes in an HTML document form a document tree (or node tree). The tree starts at the document node and continues to branch out until it has reached all text nodes at the lowest level of the tree.

The following figure represents a document tree (node tree):

DOM HTML tree

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值