1. What is DOM DOM Document Object Model is a programming interface for HTML and XML documents. It uses a logical tree to represent a document. The end point of each branch of the tree is a DOM treats an HTML document as a tree of nodes, where a node represents an HTML element. The following HTML code is provided to better understand the DOM tree structure. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>DOM tree structure</title> </head> <body> <h1>DOM Object Model</h1> <h2>DOM tree structure</h2> </body> </html> The document is called the root node and contains one child node, Both These elements in the document can be accessed and changed via 2. Select elementsHow to select elements in a document? There are several different ways to select elements in an HTML document. Here are three of them:
3. getElementById() In HTML, In <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>DOM tree structure</title> </head> <body> <h1>DOM Object Model</h1> <h2>DOM tree structure</h2> <p id="intro">DOM stands for Document Object Model, which is a programming interface. </p> <script type="text/javascript"> const elemIntro = document.getElementById("intro"); console.log(elemIntro); </script> </body> </html> Open the developer tools: If you need to get the text content of an object, you can use <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>DOM tree structure</title> </head> <body> <h1>DOM Object Model</h1> <h2>DOM tree structure</h2> <p id="intro">DOM stands for Document Object Model, which is a programming interface. </p> <script type="text/javascript"> const elemIntro = document.getElementById("intro"); console.log(elemIntro); // Get the text content within the element const elemText = elemIntro.textContent; // elemIntro.innerText console.log(elemText); </script> </body> </html> Open the controller to view: 4. querySelector() Use this method to find elements with one or more CSS selectors. The following takes the National Day movie schedule information as an example to get the corresponding title and list elements through <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>DOM tree structure</title> </head> <body> <h1>2021 National Day movie schedule</h1> <ul class="movies"> <li>Changjin Lake</li> <li>My Fathers and I</li> <li>Railway Heroes</li> </ul> <script type="text/javascript"> const elemTitle = document.querySelector("h1"); const elemList = document.querySelector(".movies"); console.log("h1"); console.log(elemTitle); console.log("ul movies"); console.log(elemList); </script> </body> </html> Open the document in the browser and turn on the developer tools, you can see the following effect: 5. querySelectorAll()This method finds all elements that match the CSS selector and returns a list of nodes. Let's find every li element of the movie schedule: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>DOM tree structure</title> </head> <body> <h1>2021 National Day movie schedule</h1> <ul class="movies"> <li>Changjin Lake</li> <li>My Fathers and I</li> <li>Railway Heroes</li> </ul> <script type="text/javascript"> const elemTitle = document.querySelector("h1"); const movieItems = document.querySelectorAll(".movies > li"); console.log(movieItems); </script> </body> </html> The effect of console printing is as follows: The above node list can be traversed as follows: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>DOM tree structure</title> </head> <body> <h1>2021 National Day movie schedule</h1> <ul class="movies"> <li>Changjin Lake</li> <li>My Fathers and I</li> <li>Railway Heroes</li> </ul> <script type="text/javascript"> const elemTitle = document.querySelector("h1"); const movieItems = document.querySelectorAll(".movies > li"); movieItems.forEach((item) => { console.log(item); }); </script> </body> </html> The effect of console printing is as follows: 6. Add new elements How do you add new elements to a document? You can use <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>DOM tree structure</title> </head> <body> <h1>2021 National Day movie schedule</h1> <ul class="movies" id="movies"> <li>Changjin Lake</li> <li>My Fathers and I</li> <li>Railway Heroes</li> </ul> <script type="text/javascript"> const movieItems = document.getElementById("movies"); const newMovie = document.createElement("li"); newMovie.textContent = "The Eagle Catches the Chicken"; movieItems.appendChild(newMovie); </script> </body> </html> After running, the page effect is as follows: 7. Change CSS stylesHow to change inline CSS styles? By using the style attribute, you can change the CSS style in the HTML document. Taking the National Day movie schedule as an example, change the font size and font color of the page title h1 element: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>DOM tree structure</title> </head> <body> <h1>2021 National Day movie schedule</h1> <ul class="movies"> <li>Changjin Lake</li> <li>My Fathers and I</li> <li>Railway Heroes</li> </ul> <script type="text/javascript"> const pageTitle = document.querySelector("h1"); pageTitle.style.fontSize = "24px"; pageTitle.style.color = "#0000FF"; </script> </body> </html> After running, the page effect is as follows: Regarding the attributes of CSS styles, camelCase is used in JavaScript. For example, the corresponding attribute of 8. How to monitor events How to use events of elements on the page? Using the <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>DOM tree structure</title> </head> <body> <h1>2021 National Day movie schedule</h1> <ul id="movies"> <li>Changjin Lake</li> <li>My Fathers and I</li> <li>Railway Heroes</li> </ul> <button id="addNew">Add the movie "Eagle Catches Chicken"</button> <script type="text/javascript"> const moviesList = document.getElementById("movies"); const button = document.getElementById("addNew"); const addNewMovie = (movieTitle, elemTarget) => { const newMovie = document.createElement("li"); newMovie.textContent = movieTitle; newMovie.style.color = "#ff0000"; elemTarget.appendChild(newMovie); }; button.addEventListener("click", () => { addNewMovie("The Eagle Catches the Chicken", moviesList); }); </script> </body> </html> After running, the page effect is as follows: In actual projects, event monitoring and processing are more complicated than this. For modern WEB development, rich front-end frameworks have already encapsulated event processing very well, and even make developers forget the relevant knowledge of event monitoring, such as event bubbling, event capture, event delegation, etc., which will not be expanded in this article. in conclusion: DOM Document Object Model, a programming interface for HTML and XML documents. When a browser first reads (parses) an This is the end of this article about the Document Object Model DOM in You may also be interested in:
|
<<: MySQL Flush-List and dirty page flushing mechanism
>>: Solve the problem of docker container exiting immediately after starting
Effect: When the slideshow moves in one direction...
Problem Description When filter attribute is used...
1. Concept 1. The difference between hot backup a...
The environment of this article is Windows 10, an...
Preface Recently, a data was operated incorrectly...
I saw in the LOFTER competition that it was mentio...
Table of contents Preface 1. 404 Page 1. Causes 2...
1. <dl> defines a list, <dt> defines ...
Requirements: The PC side and the mobile side are...
This article example shares the specific code of ...
Specific method: 1. Press [ win+r ] to open the r...
Table of contents 1. Swap partition SWAP 1.1 Crea...
In the previous article, we explained how nginx r...
In actual development, the primary key of MySQL c...
In daily development, front-end students often ar...