一、創(chuàng)建節(jié)點(diǎn)創(chuàng)建節(jié)點(diǎn)分為創(chuàng)建元素節(jié)點(diǎn)和文本節(jié)點(diǎn).
const newDiv = document.createElement('div');
newDiv.textContent = '我是新來的div';
newDiv.style.backgroundColor = 'lightblue';
newDiv.style.padding = '10px';
createElement
方法接收一個(gè)參數(shù), 就是你要?jiǎng)?chuàng)建的 HTML 標(biāo)簽名. 創(chuàng)建后, 這個(gè)元素還不會(huì)立即出現(xiàn)在頁面上, 它只是存在于內(nèi)存中, 等著被添加到 DOM 樹中.創(chuàng)建文本節(jié)點(diǎn):?const textNode = document.createTextNode('這是一段純文本內(nèi)容');
newDiv.appendChild(textNode);
雖然現(xiàn)在大多數(shù)情況下我們直接用textContent
或innerText
就夠了, 但在某些特殊場景下 createTextNode
還是很有用的.創(chuàng)建好節(jié)點(diǎn)后, 我們需要把它們放到頁面中才能看到效果:添加到末尾, 這是最常用的方法 , 把節(jié)點(diǎn)添加到指定父節(jié)點(diǎn)的子節(jié)點(diǎn)列表末尾:
const container = document.getElementById('container');
container.appendChild(newDiv);
如果你把一個(gè)已存在于文檔中的節(jié)點(diǎn) append 到另一個(gè)位置, 它會(huì)被從原來的位置移動(dòng), 而不會(huì)被復(fù)制.在指定位置插入, 如果你想控制新節(jié)點(diǎn)的位置 , 而不是總是添加到末尾, 可以使用 insertBefore:
const newParagraph = document.createElement('p');
newParagraph.textContent = '我在中間';
const firstChild = container.firstChild;
container.insertBefore(newParagraph, firstChild);
insertBefore
接收兩個(gè)參數(shù): 要插入的節(jié)點(diǎn)和參考節(jié)點(diǎn).新節(jié)點(diǎn)會(huì)被插入到參考節(jié)點(diǎn)之前.
更靈活的插入,這組方法提供了更多插入位置的選擇:
const targetElement = document.getElementById('target');
targetElement.insertAdjacentHTML('beforebegin', '<div>beforebegin - 在元素前面</div>');
targetElement.insertAdjacentHTML('afterbegin', '<div>afterbegin - 在元素內(nèi)部開頭</div>');
targetElement.insertAdjacentHTML('beforeend', '<div>beforeend - 在元素內(nèi)部末尾</div>');
targetElement.insertAdjacentHTML('afterend', '<div>afterend - 在元素后面</div>');
這個(gè)方法特別適合需要精確控制插入位置的場景.三、修改節(jié)點(diǎn), 創(chuàng)建和添加節(jié)點(diǎn)后, 我們經(jīng)常需要修改它們const link = document.createElement('a');
link.href = 'https://www.example.com';
link.title = '示例網(wǎng)站';
link.setAttribute('data-custom', 'value');
link.style.color = 'red';
link.style.textDecoration = 'none';
link.innerHTML = '<strong>點(diǎn)擊我</strong>';
link.textContent = '安全的文本內(nèi)容';
link.innerHTML += ' (追加的內(nèi)容)';
3) replaceChild - 替換節(jié)點(diǎn)
const newHeading = document.createElement('h2');
newHeading.textContent = '新的標(biāo)題';
const oldHeading = document.getElementById('old-heading');
const parent = oldHeading.parentNode;
parent.replaceChild(newHeading, oldHeading);
1)cloneNode - 節(jié)點(diǎn)克隆: const original = document.getElementById('original');
const shallowClone = original.cloneNode(false);
const deepClone = original.cloneNode(true);
document.body.appendChild(deepClone);
cloneNode
默認(rèn)不會(huì)復(fù)制事件監(jiān)聽器,但會(huì)復(fù)制屬性(包括像 onclick
這樣的內(nèi)聯(lián)事件處理器).2)removeChild - 移除節(jié)點(diǎn): const parent = document.getElementById('parent');
const child = document.getElementById('child-to-remove');
parent.removeChild(child);
const elementToRemove = document.getElementById('remove-me');
elementToRemove.remove();
3) DocumentFragment, 批量操作優(yōu)化:當(dāng)你需要添加多個(gè)節(jié)點(diǎn)時(shí),頻繁操作 DOM 會(huì)導(dǎo)致性能問題.這時(shí)可以使用 DocumentFragment:
const fragment = document.createDocumentFragment();
for (let i = 0; i < 100; i++) {
const newItem = document.createElement('li');
newItem.textContent = `項(xiàng)目 ${i}`;
fragment.appendChild(newItem);
}
document.getElementById('list').appendChild(fragment);
五、我們來看一個(gè)完整的例子, 結(jié)合上面講的各種方法
function addComment(name, text) {
const fragment = document.createDocumentFragment();
const commentDiv = document.createElement('div');
commentDiv.className = 'comment';
const nameHeading = document.createElement('h3');
nameHeading.textContent = name;
const textParagraph = document.createElement('p');
textParagraph.textContent = text;
const deleteButton = document.createElement('button');
deleteButton.textContent = '刪除';
deleteButton.className = 'delete-btn';
commentDiv.appendChild(nameHeading);
commentDiv.appendChild(textParagraph);
commentDiv.appendChild(deleteButton);
fragment.appendChild(commentDiv);
const commentsContainer = document.getElementById('comments');
if (commentsContainer.firstChild) {
commentsContainer.insertBefore(fragment, commentsContainer.firstChild);
} else {
commentsContainer.appendChild(fragment);
}
}
document.getElementById('comments').addEventListener('click', function(e) {
if (e.target.classList.contains('delete-btn')) {
const comment = e.target.closest('.comment');
comment.remove();
}
});
addComment('張三', '這篇文章太棒了!');
addComment('李四', '學(xué)到了很多新知識(shí)!');
閱讀原文:原文鏈接
該文章在 2025/7/7 9:35:17 編輯過