Learn the basics of JavaScript DOM operations in one article

Learn the basics of JavaScript DOM operations in one article

DOM Concepts

DOM: document object model: The document object model uses js to manipulate HTML code, add elements, and delete elements. . .

Get Elements

(1) getElementById gets an element by id (2) getElementsByTagName gets an element by tag name (array)
(3) getElementsByClassName gets an element set (array) by class.
(4) getElementsByName gets an element collection (array) through the name attribute

Summary : Elements can be obtained based on tag name, id, class, and name attributes. The result of Id is an element, while the result of others is a collection.
The document object supports the above four, while the element object only supports
getElementsByTagName and getElementsByClassName

Modifying Elements

(1) Modify the content. The text inside the tag can be read or set through the innerText property. The text inside the tag can be read or set through the innerHTML property. There are two differences: innerHTML will parse the text according to the rules of HTML, while innerText is just treated as ordinary text content.

insert image description here

(2) Modify the style
a:xxx.style.yyy
b: xxx.classname = "..." (equivalent to modifying the class attribute)

insert image description here

Add and remove elements

(1) createElement creates an element node
createElement("p") creates a paragraph (2) createTextNode creates a text node
createTextNode("text content") creates a text node with the value "text content" (3) appendChild adds a child node (4) removeChild removes a child node

insert image description here
insert image description here

navigation

Document: root node
parentNode: Get the parent node
childNodes: Get all child nodes
firstChild: the first child node
lastChild: the last child node

insert image description here
insert image description here

This is the end of this article about the basics of JavaScript DOM operations. For more relevant JavaScript DOM basics content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of common JavaScript DOM operation codes
  • All properties of JavaScript variable Dom object
  • Detailed explanation of common operation examples of js DOM events
  • Understand Dom operations in javascript
  • Introduction to Javascript DOM, nodes and element acquisition

<<:  MySQL 5.7.21 decompression version installation Navicat database operation tool installation

>>:  Summary of some common configurations and techniques of Nginx

Recommend

Parsing the commonly used v-instructions in vue.js

Table of contents Explanation of v-text on if for...

Shell script to monitor MySQL master-slave status

Share a Shell script under Linux to monitor the m...

Vant Uploader implements the component of uploading one or more pictures

This article shares the Vant Uploader component f...

Introduction to fourteen cases of SQL database

Data Sheet /* Navicat SQLite Data Transfer Source...

Docker meets Intellij IDEA, Java development improves productivity tenfold

Table of contents 1. Preparation before developme...

CSS3 uses animation attributes to achieve cool effects (recommended)

animation-name animation name, can have multiple ...

Several principles for website product design reference

The following analysis is about product design pr...

The images in HTML are directly replaced by base64 encoded strings

Recently, I came across a webpage that had images ...

Linux completely removes node.js and reinstalls it through the yum command

first step Delete it once with the built-in packa...

Why does MySQL database index choose to use B+ tree?

Before further analyzing why MySQL database index...

Details after setting the iframe's src to about:blank

After setting the iframe's src to 'about:b...