Home > Web Front-end > JS Tutorial > What is DOM (Document Object Modle) in JavaScript

What is DOM (Document Object Modle) in JavaScript

Mary-Kate Olsen
Release: 2024-11-27 18:30:11
Original
313 people have browsed it

What is DOM (Document Object Modle) in JavaScript

The DOM (Document Object Model) is a programming interface for web documents. It represents the structure of a webpage as a tree of objects, where each object corresponds to a part of the page, such as elements, attributes, and text content.

In JavaScript, the DOM allows you to interact with HTML or XML documents programmatically. It enables you to dynamically modify the content, structure, and style of a webpage.

Key Concepts of the DOM:

  1. Document: Refers to the entire web page.
  2. Element: Represents HTML tags such as
    ,

    , , etc.

  3. Node: A basic unit in the DOM tree, which could be an element, text, or attribute.
  4. DOM Tree: A hierarchical structure that represents the webpage as a tree of nodes. The root node is the document, and each HTML element is a node branching from it.

Example of DOM Representation:

For an HTML document like:

<!DOCTYPE html>
<html>
  <head>
    <title>My Page</title>
  </head>
  <body>
    <div>



<p>The DOM for this document might look like this:<br>
</p>

<pre class="brush:php;toolbar:false">Document
 ├── html
     ├── head
     │    └── title
     └── body
          └── div#content
               ├── h1
               └── p
Copy after login
Copy after login

How JavaScript Uses the DOM:

  1. Accessing Elements: JavaScript can select and access elements from the DOM using methods like document.getElementById() or document.querySelector().
   let heading = document.getElementById("content");
Copy after login
  1. Manipulating Elements: JavaScript can modify the content, attributes, or style of elements.
   heading.innerHTML = "New Content";
   heading.style.color = "red";
Copy after login
  1. Event Handling: JavaScript can attach event listeners to DOM elements to respond to user actions like clicks, key presses, etc.
   document.getElementById("myButton").addEventListener("click", function() {
     alert("Button clicked!");
   });
Copy after login
  1. Creating and Removing Elements: JavaScript can dynamically create new elements or remove existing ones.
   const newElement = document.createElement("div");
   document.body.appendChild(newElement);
Copy after login

Why the DOM is Important:

  • It provides a way for JavaScript to interact with and manipulate the structure and content of a webpage.
  • It allows for dynamic, interactive web pages without requiring a page reload (e.g., for updating the UI or handling user inputs).

DOM Methods:

Here are some commonly used DOM methods in JavaScript:

  • getElementById(): Selects an element by its ID.
  • querySelector(): Selects the first matching element using a CSS selector.
  • createElement(): Creates a new HTML element.
  • appendChild(): Adds a new child node to an element.
  • removeChild(): Removes a child node from an element.
  • setAttribute(): Sets an attribute on an element.
  • addEventListener(): Attaches an event handler to an element.

Example in Action:

<!DOCTYPE html>
<html>
  <head>
    <title>My Page</title>
  </head>
  <body>
    <div>



<p>The DOM for this document might look like this:<br>
</p>

<pre class="brush:php;toolbar:false">Document
 ├── html
     ├── head
     │    └── title
     └── body
          └── div#content
               ├── h1
               └── p
Copy after login
Copy after login

In this example, the h1 element is selected, its content is updated, and a click event listener is attached to it. When the heading is clicked, an alert is shown.

The DOM is an essential concept for manipulating the structure of web pages dynamically with JavaScript, and it is fundamental to creating interactive web applications.

The above is the detailed content of What is DOM (Document Object Modle) in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template