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

W3C Tutorial (6): W3C CSS Activities

A style sheet describes how a document should be ...

How to dynamically add ports to Docker without rebuilding the image

Sometimes you may need to modify or add exposed p...

Solve the problem of yum installation error Protected multilib versions

Today, when installing nginx on the cloud server,...

How to handle token expiration in WeChat Mini Programs

Table of contents Conclusion first question Solut...

A detailed introduction to for/of, for/in in JavaScript

Table of contents In JavaScript , there are sever...

Ubuntu regularly executes Python script example code

Original link: https://vien.tech/article/157 Pref...

What you need to know about msyql transaction isolation

What is a transaction? A transaction is a logical...

Comparing the performance of int, char, and varchar in MySQL

There are many seemingly true "rumors" ...

Detailed explanation of Linux netstat command

Table of contents Linux netstat command 1. Detail...

Using JS to implement binary tree traversal algorithm example code

Table of contents Preface 1. Binary Tree 1.1. Tra...

React encapsulates the global bullet box method

This article example shares the specific code of ...

Detailed explanation of EXT series file system formats in Linux

Linux File System Common hard disks are shown in ...