404 Not Found

404 Not Found


nginx

DOM Manipulation

DOM (Document Object Model) is a tree that the browser creates from the HTML document. By operating on this tree with JavaScript, you can dynamically change the page's content, structure, and attributes—like picking fruit from a tree, grafting new branches, or cutting dead wood.


Selecting Elements

The first step in DOM manipulation is always "find it first".

Method Returns Description
getElementById('id') Single element Fastest, id is unique
getElementsByClassName('cls') HTMLCollection Live collection, syncs with DOM changes
getElementsByTagName('tag') HTMLCollection Same as above
querySelector('selector') Single element CSS selector, returns first match
querySelectorAll('selector') NodeList Static collection, doesn't change with DOM
💡 querySelector family vs getElementBy*: the former uses CSS selectors (more flexible), the latter has slightly better performance. For daily development, prefer querySelector—it's more intuitive.

Example: Comparing Five Selection Methods

HTML
<div id="box" class="card active">Hello</div>
<div class="card">World</div>
<p class="card">JS</p>

<script>
const byId = document.getElementById('box');
console.log('byId:', byId.textContent);

const byClass = document.getElementsByClassName('card');
console.log('byClass count:', byClass.length);

const byTag = document.getElementsByTagName('p');
console.log('byTag:', byTag[0].textContent);

const first = document.querySelector('.card');
console.log('querySelector:', first.textContent);

const all = document.querySelectorAll('.card');
console.log('querySelectorAll count:', all.length);
</script>
▶ Try it Yourself

Modifying Content

After finding an element, the most common need is to change "what it says".

⚠️ Never use innerHTML to insert user-supplied content, otherwise attackers can inject malicious scripts. It's like posting a letter from a stranger directly to the bulletin board—the letter might contain a "bomb".

Example: textContent vs innerHTML

HTML
<div id="safe"></div>
<div id="danger"></div>

<script>
const safe = document.getElementById('safe');
safe.textContent = '<b>This will not be bold</b>';

const danger = document.getElementById('danger');
danger.innerHTML = '<b>This will be bold</b>';
</script>
▶ Try it Yourself

Modifying Attributes

HTML element attributes (id, class, src, href, etc.) can also be read and written with JS.

Method Description
getAttribute('name') Read attribute value
setAttribute('name', 'value') Set attribute
removeAttribute('name') Remove attribute
💡 Some attributes can be accessed directly with dot notation: el.id, el.className, el.src. But for custom attributes (data-*), use getAttribute or el.dataset.

HTML
<a id="link" href="https://example.com" target="_blank">Example Link</a>

<script>
const link = document.getElementById('link');

console.log('href:', link.getAttribute('href'));

link.setAttribute('href', 'https://mdn.io');
link.setAttribute('title', 'Click to go to MDN');

link.removeAttribute('target');

console.log('After modification:', link.getAttribute('href'));
</script>
▶ Try it Yourself

Creating and Inserting Elements

One of the core DOM manipulation abilities: "create" an element out of thin air, then attach it to the tree.

Method Description
createElement('tag') Create element node
createTextNode('text') Create text node
appendChild(child) Append to end
insertBefore(new, ref) Insert before ref

Example: Dynamically Adding List Items

HTML
<ul id="list">
  <li>Existing item</li>
</ul>

<script>
const list = document.getElementById('list');

const li = document.createElement('li');
li.textContent = 'Newly added item';
list.appendChild(li);

const li2 = document.createElement('li');
li2.textContent = 'Inserted at the beginning';
list.insertBefore(li2, list.firstChild);
</script>
▶ Try it Yourself

Removing and Cloning Elements

Method Description
removeChild(child) Parent removes child node
remove() Element removes itself (newer API)
cloneNode(true/false) true = deep clone (including children), false = shallow clone
💡 remove() is simpler than removeChild(), but if you need to get the removed node for further processing, use removeChild()—it returns the removed node.

Example: Removing and Cloning

HTML
<ul id="fruits">
  <li>Apple</li>
  <li>Banana</li>
  <li>Orange</li>
</ul>

<script>
const fruits = document.getElementById('fruits');

const banana = fruits.children[1];
const removed = fruits.removeChild(banana);
console.log('Removed:', removed.textContent);

const clone = fruits.cloneNode(true);
document.body.appendChild(clone);
console.log('Cloned the list');
</script>
▶ Try it Yourself

📖 Summary

  1. Two camps for selecting elements: getElementBy* (better performance) and querySelector* (more flexible). For daily use, querySelector is sufficient.
  2. Use textContent for text changes, use innerHTML only when you need HTML. The latter requires XSS prevention.
  3. Attribute operations: getAttribute to read, setAttribute to write, removeAttribute to delete.
  4. Standard flow for creating elements: createElement → set content/attributes → appendChild to mount.
  5. Two ways to remove elements: remove() (element removes itself), removeChild() (parent removes it, and you get the node back).
  6. cloneNode(true) deep-clones including all descendants, cloneNode(false) clones only the shell.

❓ FAQ

Q What happens if querySelector returns null?
A Calling any property/method on null will throw a TypeError. So it's best to check before operating: const el = document.querySelector('.xxx'); if (el) { ... }.
Q What's the difference between HTMLCollection and NodeList?
A HTMLCollection is live—it automatically updates when the DOM changes; NodeList from querySelectorAll is a static snapshot. When iterating and adding/removing elements, a static collection is safer, otherwise you might get an infinite loop.
Q What happens if you appendChild a node that already exists?
A The node "moves" from its original position to the new position. In the DOM, the same node can only appear once. This is a move, not a copy.

📝 Exercises

  1. Basic: Create a page that uses createElement and appendChild to dynamically generate a list containing 5 city names.
  2. Intermediate: Add a delete button to the list from the previous exercise. When clicked, use removeChild to delete the corresponding list item.
  3. Challenge: Implement a simple "note manager"—an input field + add button. Each time you add a note, create a card with a delete button. Clicking the delete button removes that card.
100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏