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

The meaning and calculation method of QPS and TPS of MySQL database

When doing DB benchmark testing, qps and tps are ...

Detailed explanation of MySQL data grouping

Create Group Grouping is established in the GROUP...

Example code of html formatting json

Without further ado, I will post the code for you...

In-depth understanding of this in JavaScript

In-depth understanding of this in Js JavaScript s...

JavaScript ES new feature block scope

Table of contents 1. What is block scope? 2. Why ...

A simple method to regularly delete expired data records in MySQL

1. After connecting and logging in to MySQL, firs...

Ajax jquery realizes the refresh effect of a div on the page

The original code is this: <div class='con...

How to set Nginx log printing post request parameters

【Foreword】 The SMS function of our project is to ...

How to install Linux flash

How to install flash in Linux 1. Visit the flash ...

Example of using rem to replace px in vue project

Table of contents tool Install the plugin Add a ....

Detailed explanation of for loop and double for loop in JavaScript

for loop The for loop loops through the elements ...

Learn the black technology of union all usage in MySQL 5.7 in 5 minutes

Performance of union all in MySQL 5.6 Part 1:MySQ...

Implementation of check constraints in MySQL 8.0

Hello everyone, I am Tony, a teacher who only tal...