Lädt...


🔧 Understanding the Document Object Model (DOM)


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

The Document Object Model (DOM) is a crucial concept for web developers, as it defines the structure of a document and allows for dynamic interaction with HTML and XML documents. This guide will help you understand the DOM, its structure, and how to manipulate it using JavaScript.

What is the DOM?

  • Definition: The DOM is a programming interface for web documents. It represents the document as a tree of objects, allowing scripts to update the content, structure, and style of a document while it's being viewed.
  • Tree Structure: The DOM represents a document as a tree of nodes. Each node represents a part of the document, such as an element, attribute, or text.

DOM Structure

Node Types

  1. Document Node: The root of the DOM tree. Represents the entire document.
  2. Element Node: Represents an HTML element (e.g., <div>, <p>, <a>).
  3. Text Node: Represents the text within an element.
  4. Attribute Node: Represents the attributes of an element (e.g., class, id).
  5. Comment Node: Represents comments in the HTML.

Example DOM Tree

<!DOCTYPE html>
<html>
<head>
    <title>My Page</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <p>This is a paragraph.</p>
</body>
</html>

DOM Representation:

Document
├── html (Element)
│   ├── head (Element)
│   │   └── title (Element)
│   │       └── "My Page" (Text)
│   └── body (Element)
│       ├── h1 (Element)
│       │   └── "Hello, World!" (Text)
│       └── p (Element)
│           └── "This is a paragraph." (Text)

Accessing the DOM

Selecting Elements

  • getElementById: Selects a single element by its ID.
  const element = document.getElementById('myId');
  • getElementsByClassName: Returns a live HTMLCollection of elements with the specified class name.
  const elements = document.getElementsByClassName('myClass');
  • getElementsByTagName: Returns a live HTMLCollection of elements with the specified tag name.
  const elements = document.getElementsByTagName('div');
  • querySelector: Selects the first element that matches a CSS selector.
  const element = document.querySelector('.myClass');
  • querySelectorAll: Returns a static NodeList of all elements that match a CSS selector.
  const elements = document.querySelectorAll('div.myClass');

Manipulating Elements

  • Changing Content:
  const element = document.getElementById('myId');
  element.textContent = 'New Content';
  • Changing Attributes:
  const element = document.getElementById('myId');
  element.setAttribute('class', 'newClass');
  • Changing Styles:
  const element = document.getElementById('myId');
  element.style.color = 'blue';
  • Creating and Appending Elements:
  const newElement = document.createElement('div');
  newElement.textContent = 'I am a new div';
  document.body.appendChild(newElement);
  • Removing Elements:
  const element = document.getElementById('myId');
  element.parentNode.removeChild(element);

DOM Events

Events are actions or occurrences that happen in the browser, and you can respond to them with event handlers.

Adding Event Listeners

const button = document.getElementById('myButton');
button.addEventListener('click', function() {
    alert('Button clicked!');
});

Common Events

  • click: Triggered when an element is clicked.
  • mouseover: Triggered when the mouse hovers over an element.
  • keydown: Triggered when a key is pressed down.
  • submit: Triggered when a form is submitted.

Conclusion

Understanding the DOM is essential for web development, as it provides a way to interact with and manipulate web pages. Mastering DOM manipulation will allow you to create dynamic and interactive web applications.

Feel free to dive deeper into the documentation and experiment with the various methods and properties available in the DOM API. Happy coding!

...

🔧 Understanding the Document Object Model (DOM)


📈 43.07 Punkte
🔧 Programmierung

🔧 Understanding DOM (Document Object Model)


📈 43.07 Punkte
🔧 Programmierung

🔧 Mastering Selenium C# with NUnit: In-Depth Guide to Page Object Model (POM) and Data Object Model (DOM)


📈 41.55 Punkte
🔧 Programmierung

🔧 UNVEILING THE WEB MAGIC OF DOM (Document Object Model)


📈 36.16 Punkte
🔧 Programmierung

🔧 The Document Object Model (DOM)


📈 36.16 Punkte
🔧 Programmierung

🔧 JavaScript in the Browser – How the Document Object Model (DOM) and Events Work


📈 36.16 Punkte
🔧 Programmierung

🔧 What is the Document Object Model? DOM for Beginners


📈 36.16 Punkte
🔧 Programmierung

🔧 How the Document Object Model Works in JavaScript – DOM Tutorial for Beginners


📈 36.16 Punkte
🔧 Programmierung

🔧 Document Object Model (DOM) Geometry: A Beginner’s Introduction And Guide


📈 36.16 Punkte
🔧 Programmierung

🔧 DOM - Document object Model


📈 36.16 Punkte
🔧 Programmierung

🔧 DOM – Document Object Model.


📈 36.16 Punkte
🔧 Programmierung

🔧 The Document Object Model (DOM)- A Complete Guide


📈 36.16 Punkte
🔧 Programmierung

🔧 Có thể bạn chưa biết (Phần 1)


📈 34.7 Punkte
🔧 Programmierung

🔧 Tìm Hiểu Về RAG: Công Nghệ Đột Phá Đang "Làm Mưa Làm Gió" Trong Thế Giới Chatbot


📈 34.7 Punkte
🔧 Programmierung

🔧 Shadow DOM vs Virtual DOM: Understanding the Key Differences


📈 32 Punkte
🔧 Programmierung

🔧 Understanding Reconciliation in ReactJs - Keeping the Virtual DOM and the Real DOM in Sync.


📈 32 Punkte
🔧 Programmierung

🕵️ Google Chrome 31.0.1650.63 Frame Viewer dom/Document.cpp Document::updateLayout use after free


📈 30.78 Punkte
🕵️ Sicherheitslücken

🔧 What is DOM (Document Object Modle) in JavaScript


📈 29.92 Punkte
🔧 Programmierung

🔧 Domain Object Model (DOM)


📈 27.05 Punkte
🔧 Programmierung

🔧 Mastering JavaScript: Unveiling the Power of DOM Manipulation and the JavaScript Object Model


📈 27.05 Punkte
🔧 Programmierung

🔧 Difference between a virtual DOM and a real DOM


📈 25.09 Punkte
🔧 Programmierung

🔧 Difference Between DOM and Virtual DOM


📈 25.09 Punkte
🔧 Programmierung

🕵️ Mozilla Firefox bis 46 DOM Element Handler mozilla::dom::Element Pufferüberlauf


📈 25.09 Punkte
🕵️ Sicherheitslücken

🕵️ Mozilla Firefox bis 46 DOM Element Handler mozilla::dom::Element Pufferüberlauf


📈 25.09 Punkte
🕵️ Sicherheitslücken

🔧 Virtual DOM and Actual DOM with Analogy Explanation


📈 25.09 Punkte
🔧 Programmierung

🔧 Virtual DOM and Actual DOM with Analogy Explanation


📈 25.09 Punkte
🔧 Programmierung

🔧 Dom and Virtual Dom


📈 25.09 Punkte
🔧 Programmierung

🔧 Actual DOM and Virtual DOM in Javascript


📈 25.09 Punkte
🔧 Programmierung

🔧 Real DOM vs Virtual DOM


📈 25.09 Punkte
🔧 Programmierung

🔧 DOM Manipulation: Selecting and Manipulating DOM Elements


📈 25.09 Punkte
🔧 Programmierung

🔧 Efficient DOM Manipulation with the Virtual DOM and Refs


📈 25.09 Punkte
🔧 Programmierung

🔧 Deciphering the Origins: Why the DOM is called the DOM?


📈 25.09 Punkte
🔧 Programmierung

🔧 Deciphering the Origins: Why the DOM is called the DOM?


📈 25.09 Punkte
🔧 Programmierung

🔧 Mythbusting DOM: Is DOM the same as HTML?


📈 25.09 Punkte
🔧 Programmierung

matomo