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:
children
and childElementCount
properties, avoiding the inclusion of non-element nodes (as childNodes.length
might).hasChildNodes()
to check for any child nodes, including whitespace (a valid node type).appendChild()
and remove them using removeChild()
or ChildNode.remove()
.replaceChild()
, enabling element swapping and repositioning.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>
To count elements within the <code><ul>
:
var myList = document.getElementById('myList'); console.log(myList.children.length); // 6 console.log(myList.childElementCount); // 6
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
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);
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);
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!