DOM Tree
Browser Document: DOM Tree
What is the DOM tree?
View Answer:
What is an HTML tag considered in the Document Object Model?
View Answer:
document.body.style.background = 'red'; // make the background red
setTimeout(() => (document.body.style.background = ''), 3000); // return back
console.log(document.body); // console.logs [object HTMLBodyElement]
How does the Document Object Model represent HTML?
View Answer:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<!-- Parent DIV -->
<div id="parent">
<!-- Child DIV -->
<div id="child"></div>
</div>
</body>
</html>
If the browser encounters malformed HTML, what happens?
View Answer:
<!-- Malformed HTML before DOM generation -->
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<div>
<h1>Heading</h1>
<p>This is a paragraph without a closing tag
</div>
</body>
</html>
<!-- Fixed, After DOM generation -->
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<div>
<h1>Heading</h1>
<!-- paragraph tag fixed after dom generation -->
<p>This is a paragraph without a closing tag</p>
</div>
</body>
</html>
Do HTML tables always have a <tbody>
in the DOM?
View Answer:
<!-- Before DOM generation -->
<table id="table">
<tr>
<td>1</td>
</tr>
</table>
<!-- Now, After DOM generation -->
<table id="table">
<tbody>
<tr>
<td>1</td>
</tr>
</tbody>
</table>
What are the different types of DOM nodes?
View Answer:
<!DOCTYPE html>
<html>
<head>
<title>Learning About Nodes</title>
</head>
<body>
<h1>An element node</h1>
<!-- a comment node -->
A text node.
</body>
</html>
What is event propagation in the context of the DOM?
View Answer:
// This will trigger during the bubbling phase (default behavior)
element.addEventListener('click', function() {
console.log('Bubble phase');
}, false);
// This will trigger during the capturing phase
element.addEventListener('click', function() {
console.log('Capture phase');
}, true);
Can you explain the Shadow DOM?
View Answer:
What is the Light DOM?
View Answer:
Here's an example of Light DOM content...
<my-web-component>
<p>This is light DOM content</p>
</my-web-component>
The paragraph (<p>
) is a Light DOM child of the <my-web-component>
custom element.
Shadow DOM, on the other hand, is a way of creating an encapsulated tree of DOM nodes that are separate from the main (light) DOM tree. This means the structure, styles, and behavior inside the Shadow DOM are scoped to the Shadow DOM and do not bleed out to the main document, and vice versa. This feature is very useful for building self-contained, reusable components without worrying about style or script conflicts with the rest of the document.
Here's an example of how you might add some Shadow DOM content to a custom element:
class MyWebComponent extends HTMLElement {
connectedCallback() {
let shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.innerHTML = `<p>This is shadow DOM content</p>`;
}
}
customElements.define('my-web-component', MyWebComponent);
In this example, the Shadow DOM content is not directly accessible through the main DOM tree, and it's not affected by styles and scripts outside its shadow tree. This is in contrast to the Light DOM content, which is directly part of the main DOM tree and fully exposed to its styles and scripts.
In web development, the term "Light DOM" is often used in contrast to the "Shadow DOM" when discussing Web Components.