Home  >  Article  >  Web Front-end  >  Summary of JavaScript knowledge points: DOM

Summary of JavaScript knowledge points: DOM

WBOY
WBOYforward
2022-07-19 13:39:091748browse

This article brings you relevant knowledge about javascript, which mainly introduces related issues about DOM. The Document Object Model (DOM) is recommended by the W3C organization The standard programming interface for handling extensible markup language. Let’s take a look at it. I hope it will be helpful to everyone.

Summary of JavaScript knowledge points: DOM

[Related recommendations: javascript video tutorial, web front-end

1. Introduction to DOM

1.1 What is DOM

A complete JavaScript implementation consists of the following 3 different parts:

  • Core (ECMAScript): The core part of the language, describes The language's syntax and basic objects.

  • Document Object Model (DOM): Web page document operation standard, describing methods and interfaces for processing web page content.

  • Browser Object Model (BOM): The basis for client and browser window operations, describing the methods and interfaces for interacting with the browser.

Document Object Model (Document Object Model, referred to as DOM) is a standard recommended by the W3C organization for processing extensible markup language (HTML or XML) Programming interface.
W3C has defined a series of DOM interfaces through which the content, structure and style of web pages can be changed.

Official language: Document Object Model (DOM) is a standard programming interface recommended by the W3C organization for processing extensible markup languages. It is a platform- and language-independent application programming interface (API) that can dynamically access programs and scripts and update their content, structure, and style of www documents (HTML and XML documents are defined through description sections). The document can be further processed and the results of the processing can be added to the current page. DOM is a tree-based API document that requires the entire document to be represented in memory during processing. Another simple API is event-based SAX, which can be used to process very large XML documents. Because they are large, they are not suitable for processing all in memory.

1.2 DOM tree

Summary of JavaScript knowledge points: DOM

  • Document: A page is a document, and document is used in DOM to represent
  • elements: in the page All tags are elements, and element is used in DOM to represent
  • nodes: all content in the web page is nodes (labels, attributes, text, comments, etc.), and node is used in DOM to represent

DOM Treat all the above contents as objects

2. Get elements

2.1 How to get page elements

DOM is mainly used to manipulate elements in our actual development.
How do we get the elements in the page? You can use the following methods to get the elements in the page:

  • Get based on ID
  • Get based on tag name
  • Get through the new method of HTML5
  • Get special elements

2.2 Get based on ID

Use the getElementById() method to get Element object of ID.

 document.getElementById('id');

Using console.dir() can print the element object we obtained and better view the properties and methods in the object.

2.3 Get based on the tag name

Use the getElementsByTagName() method to return a collection of objects with the specified tag name.

document.getElementsByTagName('标签名');

Note:

  1. Because what we get is a collection of objects, we need to traverse if we want to operate the elements inside.
  2. The element object obtained is dynamic

2.4 Obtain through the new method of HTML5

document.getElementsByClassName(‘类名’);// 根据类名返回元素对象集合
document.querySelector('选择器'); // 根据指定选择器返回第一个元素对象
document.querySelectorAll('选择器'); // 根据指定选择器返回

Note:
The selectors in querySelector and querySelectorAll need to be marked
For example: document.querySelector('#nav');


2.5 Get special elements (body, html)

1. Get the body element

doucumnet.body // 返回body元素对象

2. Get the html element

document.documentElement // 返回html元素对象

3. Event basics

3.1 Event overview

JavaScript enables us to Create dynamic pages, and events are behaviors that can be detected by JavaScript.
Simple understanding: Trigger --- response mechanism.
Each element in the web page can generate certain events that can trigger JavaScript. For example, we can generate an event when the user clicks a button, and then perform certain operations.

3.2 Three elements of events

  1. Event source (who)
  2. Event type (what event)
  3. Event handler (what to do)

Case:

var btn = document.getElementById('btn');
btn.onclick = function() {
 alert('你好吗'); 
};

3.3 执行事件的步骤

  1. 获取事件源
  2. 注册事件(绑定事件)
  3. 添加事件处理程序(采取函数赋值形式)

3.3 常见的鼠标事件

注:以下图片的事件只是常见的并不代表所有
Summary of JavaScript knowledge points: DOM

3.4 分析事件三要素

事件三要素:
1.事件源(你是要对什么东西进行操作)

2.事件(你要做什么实现所要的交互效果)

3.处理函数(在事件进行后你要目标变成什么样子)

四、操作元素

JavaScript 的 DOM 操作可以改变网页内容、结构和样式,我们可以利用 DOM 操作元素来改变元素里面的内容 、属性等。注意以下都是属性

4.1 改变元素内容

element.innerText

从起始位置到终止位置的内容, 但它去除 html 标签, 同时空格和换行也会去掉

element.innerHTML

起始位置到终止位置的全部内容,包括 html 标签,同时保留空格和换行

4.2 常用元素的属性操作

innerText、innerHTML 改变元素内容
src、href
id、alt、title

4.3 表单元素的属性操作

利用 DOM 可以操作如下表单元素的属性:

type、value、checked、selected、disabled

4.4 样式属性操作

我们可以通过 JS 修改元素的大小、颜色、位置等样式。

  1. element.style 行内样式操作
  2. element.className 类名样式操作

注意:
1.JS 里面的样式采取驼峰命名法 比如 fontSize、 backgroundColor
2.JS 修改 style 样式操作,产生的是行内样式,CSS 权重比较高

操作元素是 DOM 核心内容
Summary of JavaScript knowledge points: DOM

4.5 排他思想

Summary of JavaScript knowledge points: DOM

如果有同一组元素,我们想要某一个元素实现某种样式, 需要用到循环的排他思想算法:

  1. 所有元素全部清除样式(干掉其他人)
  2. 给当前元素设置样式 (留下我自己)
  3. 注意顺序不能颠倒,首先干掉其他人,再设置自己

4.6 自定义属性的操作

1. 获取属性值

  • element.属性 获取属性值。
  • element.getAttribute(‘属性’);

区别:

  • element.属性 获取内置属性值(元素本身自带的属性)
  • element.getAttribute(‘属性’); 主要获得自定义的属性 (标准) 我们程序员自定义的属性

2. 设置属性值

  • element.属性 = ‘值’ 设置内置属性值。
  • element.setAttribute(‘属性’, ‘值’);

区别:

  • element.属性 设置内置属性值
  • element.setAttribute(‘属性’); 主要设置自定义的属性 (标准)

3. 移除属性

  • element.removeAttribute(‘属性’);

4.7 H5自定义属性

自定义属性目的:是为了保存并使用数据。有些数据可以保存到页面中而不用保存到数据库中。

自定义属性获取是通过getAttribute(‘属性’) 获取。
但是有些自定义属性很容易引起歧义,不容易判断是元素的内置属性还是自定义属性。

H5给我们新增了自定义属性:

1.设置H5自定义属性

H5规定自定义属性data-开头做为属性名并且赋值。

或者使用 JS 设置

element.setAttribute(‘data-index’, 2)

2. 获取H5自定义属性
兼容性获取 element.getAttribute(‘data-index’);
H5新增 element.dataset.index 或者 element.dataset[‘index’] ie 11才开始支持

五、节点操作

获取元素通常使用两种方式:
1. 利用 DOM 提供的方法获取元素

  • document.getElementById()
  • document.getElementsByTagName()
  • document.querySelector 等  逻辑性不强、繁琐

2. 利用节点层级关系获取元素

  • 利用父子兄节点关系获取元素
  • 逻辑性强, 但是兼容性稍差

这两种方式都可以获取元素节点,后面都会使用,但是节点操作更简单

5.2 节点概述

网页中的所有内容都是节点(标签、属性、文本、注释等),在DOM 中,节点使用 node 来表示。
HTML DOM 树中的所有节点均可通过 JavaScript 进行访问,所有 HTML 元素(节点)均可被修改,也可以创建或删除

Summary of JavaScript knowledge points: DOM
一般地,节点至少拥有nodeType(节点类型)、nodeName(节点名称)和nodeValue(节点值)这三个基本属性。

  • 元素节点 nodeType 为 1
  • 属性节点 nodeType 为 2
  • 文本节点 nodeType 为 3 (文本节点包含文字、空格、换行等)

我们在实际开发中,节点操作主要操作的是元素节点

5.3 节点层级

利用 DOM 树可以把节点划分为不同的层级关系,常见的是父子兄层级关系
Summary of JavaScript knowledge points: DOM
1. 父级节点

node.parentNode

  • parentNode 属性可返回某节点的父节点,注意是最近的一个父节点
  • 如果指定的节点没有父节点则返回 null

2. 子节点

parentNode.childNodes(标准)

parentNode.childNodes 返回包含指定节点的子节点的集合,该集合为即时更新的集合。
注意:返回值里面包含了所有的子节点,包括元素节点,文本节点等。
如果只想要获得里面的元素节点,则需要专门处理。 所以我们一般不提倡使用childNodes

var ul = document. querySelector(‘ul’);for(var i = 0; i 
  1. parentNode.children(非标准)

parentNode.children 是一个只读属性,返回所有的子元素节点。它只返回子元素节点,其余节点不返回 (这个是我们重点掌握的)。
虽然children 是一个非标准,但是得到了各个浏览器的支持,因此我们可以放心使用

  1. parentNode.firstChild

firstChild 返回第一个子节点,找不到则返回null。同样,也是包含所有的节点。

  1. parentNode.lastChild

lastChild 返回最后一个子节点,找不到则返回null。同样,也是包含所有的节点

  1. parentNode.firstElementChild

firstElementChild 返回第一个子元素节点,找不到则返回null。

  1. parentNode.lastElementChild

lastElementChild 返回最后一个子元素节点,找不到则返回null。
注意:这两个方法有兼容性问题,IE9 以上才支持

实际开发中,firstChild 和 lastChild 包含其他节点,操作不方便
而 firstElementChild 和lastElementChild 又有兼容性问题,那么我们如何获取第一个子元素节点或最后一个子元素节点呢?
解决方案:

  1. 如果想要第一个子元素节点,可以使用 parentNode.chilren[0]
  2. 如果想要最后一个子元素节点,可以使用

parentNode.chilren[parentNode.chilren.length - 1]

总结

文档对象模型(Document Object Model,简称 DOM),是 W3C 组织推荐的处理可扩展标记语言(HTML或者XML)的标准编程接口。

W3C 已经定义了一系列的 DOM 接口,通过这些 DOM 接口可以改变网页的内容、结构和样式。

对于JavaScript,为了能够使JavaScript操作HTML,JavaScript就有了一套自己的dom编程接口。

对于HTML,dom使得html形成一棵dom树. 包含 文档、元素、节点

我们获取过来的DOM元素是一个对象(object),所以称为 文档对象模型

关于dom操作,我们主要针对于元素的操作。主要有创建、增、删、改、查、属性操作、事件操作。
Summary of JavaScript knowledge points: DOM

【相关推荐:javascript视频教程web前端

The above is the detailed content of Summary of JavaScript knowledge points: DOM. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete