Home > Web Front-end > JS Tutorial > DOM Tips and Techniques: Parent, Child, and Siblings

DOM Tips and Techniques: Parent, Child, and Siblings

尊渡假赌尊渡假赌尊渡假赌
Release: 2025-02-18 11:23:09
Original
460 people have browsed it

DOM Tips and Techniques: Parent, Child, and Siblings

Modern web applications often involve complex, markup-rich HTML. Libraries like jQuery simplify DOM manipulation, offering cross-browser compatibility and a wide range of features. However, native DOM APIs are significantly improved compared to a few years ago, making them a viable alternative in many cases. This article explores key DOM features for manipulating HTML, focusing on parent, child, and sibling node relationships. While jQuery remains a popular choice due to historical inconsistencies, understanding native DOM methods is crucial for efficient development.

Key Concepts:

  • Efficiently count child elements using the children and childElementCount properties, avoiding the inclusion of non-element nodes (as childNodes.length might).
  • Utilize hasChildNodes() to check for any child nodes, including whitespace (a valid node type).
  • Add or reposition elements with appendChild() and remove them using removeChild() or ChildNode.remove().
  • Seamlessly replace child elements using replaceChild(), enabling element swapping and repositioning.
  • Target specific children with firstElementChild, lastElementChild, nextElementSibling, and previousElementSibling for precise DOM manipulation.

Counting Child Nodes:

Consider this HTML example (used throughout the article):

<ul id="myList">
  <li>Example one</li>
  <li>Example two</li>
  <li>Example three</li>
  <li>Example four</li>
  <li>Example five</li>
  <li>Example six</li>
</ul>
Copy after login

To count elements within the <code><ul>:

var myList = document.getElementById('myList');
console.log(myList.children.length); // 6
console.log(myList.childElementCount); // 6
Copy after login

children.length and childElementCount yield the same result (6). childElementCount is often preferred for its clarity. Using childNodes.length would return a higher number, as it includes all node types, including whitespace.

Checking for Child Nodes:

hasChildNodes() returns a boolean indicating the presence of child nodes:

console.log(myList.hasChildNodes()); // true
Copy after login

Even an empty <code><ul> with whitespace will return true. Only a completely empty <code><ul> (no whitespace) will return false.

Adding and Removing Elements:

appendChild() adds new or moves existing elements. For example, moving the <code><ul>:

// ... (Assuming a containing div with id "container") ...
var myList = document.getElementById('myList');
document.getElementById('container').appendChild(myList);
Copy after login

removeChild() removes a child node. ChildNode.remove() provides a more concise alternative (not supported in older IE).

Replacing Elements:

replaceChild() replaces one child with another:

var myPar = document.getElementById('par'),
    myDiv = document.createElement('div');
myDiv.textContent = 'New element';
document.body.replaceChild(myDiv, myPar);
Copy after login

This replaces the <p id="par"></p> with the new <div>.<p><strong>Targeting Specific Children:</strong></p> <p><code>firstElementChild, lastElementChild, nextElementSibling, and previousElementSibling allow precise targeting of specific children.

Injecting Content:

insertBefore() inserts an element before a specified sibling. insertAdjacentHTML() offers more precise control over insertion points (beforebegin, afterbegin, beforeend, afterend).

Browser Support:

Most methods have excellent browser support, including older IE versions. ChildNode.remove() lacks support in older IE.

Conclusion:

While jQuery simplifies DOM manipulation, understanding native DOM APIs is essential for efficient and modern web development. Thorough testing is crucial to account for potential browser inconsistencies.

The above is the detailed content of DOM Tips and Techniques: Parent, Child, and Siblings. For more information, please follow other related articles on the PHP Chinese website!

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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template