).
Text Node: Represents the text within an element.
Attribute Node: Represents the attributes of an element (e.g., class, id).
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>
Salin selepas log masuk
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)
Salin selepas log masuk
Accessing the DOM
Selecting Elements
-
getElementById: Selects a single element by its ID.
const element = document.getElementById('myId');
Salin selepas log masuk
-
getElementsByClassName: Returns a live HTMLCollection of elements with the specified class name.
const elements = document.getElementsByClassName('myClass');
Salin selepas log masuk
-
getElementsByTagName: Returns a live HTMLCollection of elements with the specified tag name.
const elements = document.getElementsByTagName('div');
Salin selepas log masuk
-
querySelector: Selects the first element that matches a CSS selector.
const element = document.querySelector('.myClass');
Salin selepas log masuk
-
querySelectorAll: Returns a static NodeList of all elements that match a CSS selector.
const elements = document.querySelectorAll('div.myClass');
Salin selepas log masuk
Manipulating Elements
const element = document.getElementById('myId');
element.textContent = 'New Content';
Salin selepas log masuk
const element = document.getElementById('myId');
element.setAttribute('class', 'newClass');
Salin selepas log masuk
const element = document.getElementById('myId');
element.style.color = 'blue';
Salin selepas log masuk
-
Creating and Appending Elements:
const newElement = document.createElement('div');
newElement.textContent = 'I am a new div';
document.body.appendChild(newElement);
Salin selepas log masuk
const element = document.getElementById('myId');
element.parentNode.removeChild(element);
Salin selepas log masuk
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!');
});
Salin selepas log masuk
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!
Atas ialah kandungan terperinci Memahami Model Objek Dokumen (DOM). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!