DOM 插入操作是前端基礎技能,但 append、appendTo、appendChild 三者參數順序、回傳值、行為細節差異大。本文徹底釐清並提供實務指南。
| 方法 | 所屬 | 語法 | 參數順序 | 回傳值 | 參數類型 |
|---|---|---|---|---|---|
append() | jQuery | $(target).append(content) | 容器 → 內容 | 容器 jQuery 物件(鏈式) | 字串、DOM、jQuery、陣列 |
appendTo() | jQuery | $(content).appendTo(target) | 內容 → 容器 | 內容 jQuery 物件 | jQuery 物件 |
appendChild() | 原生 DOM | parent.appendChild(node) | 容器 → 節點 | 被插入的節點 | 單一 Node 物件 |
<div id="container"></div>
<script>
// jQuery append:目標在前,內容在後
$('#container').append('新段落
');
$('#container').append($('').text('jQuery 物件'));
$('#container').append(document.createTextNode('原生文字節點'));
// jQuery appendTo:內容在前,目標在後
$('新段落
').appendTo('#container');
$('').text('jQuery 物件').appendTo('#container');
// 原生 appendChild:只接受 Node
const p = document.createElement('p');
p.textContent = '原生元素';
document.getElementById('container').appendChild(p);
// ❌ 錯誤:appendChild 不接受字串
// document.getElementById('container').appendChild('錯誤
');
// append 回傳容器 → 可繼續操作容器
$('#box').append('1
').append('2
').addClass('done');
// appendTo 回傳內容 → 可繼續操作新增的內容
$('1
').appendTo('#box').addClass('new');
$('2
').appendTo('#box').addClass('new');
// appendChild 回傳節點本身
const p = document.createElement('p');
const appended = container.appendChild(p);
appended.classList.add('new'); // 操作新增的節點
const $existing = $('#existingElement');
// append/appendTo:若元素已在 DOM 中,會「移動」而非複製
$('#target1').append($existing); // 移到 target1
$('#target2').append($existing); // 再移到 target2(從 target1 消失)
// 複製需用 clone()
$('#target1').append($existing.clone());
$('#target2').append($existing.clone());
// 原生同樣是移動
const el = document.getElementById('existing');
target1.appendChild(el); // 移動
target2.appendChild(el); // 再移動
// 複製用 cloneNode(true)
target1.appendChild(el.cloneNode(true));
// jQuery:支援陣列、多參數
$('#container').append('1
', '2
', $('3
'));
// 原生:需用 DocumentFragment 或循環
const fragment = document.createDocumentFragment();
['1', '2', '3'].forEach(text => {
const p = document.createElement('p');
p.textContent = text;
fragment.appendChild(p);
});
container.appendChild(fragment); // 一次掛載,單次重排
| 位置 | jQuery (容器為主) | jQuery (內容為主) | 原生 |
|---|---|---|---|
| 內容開頭 | prepend() | prependTo() | prepend() (現代) / insertBefore(firstChild) |
| 內容結尾 | append() | appendTo() | append() / appendChild() |
| 元素之前 | before() | insertBefore() | before() / insertBefore() |
| 元素之後 | after() | insertAfter() | after() / insertAfter() (需自行實作) |
// 父元素方法(支援多參數、字串自動建立文字節點)
parent.append(...nodes); // 結尾
parent.prepend(...nodes); // 開頭
parent.before(...nodes); // 之前
parent.after(...nodes); // 之後
// 子元素方法
child.remove(); // 移除自己
child.replaceWith(...nodes); // 取代自己
// 範例
const container = document.getElementById('container');
container.append('文字', document.createElement('hr'), $('jQ')[0]);
const container = document.getElementById('container');
const frag = document.createDocumentFragment();
console.time('appendChild + fragment');
for (let i = 0; i < 10000; i++) {
const div = document.createElement('div');
div.textContent = i;
frag.appendChild(div);
}
container.appendChild(frag);
console.timeEnd('appendChild + fragment'); // ~5-10ms
console.time('jQuery append');
const $container = $('#container').empty();
for (let i = 0; i < 10000; i++) {
$container.append(`${i}`);
}
console.timeEnd('jQuery append'); // ~50-100ms
console.time('jQuery appendTo');
const $frag = $('');
for (let i = 0; i < 10000; i++) {
$frag.append(`${i}`);
}
$('#container').append($frag);
console.timeEnd('jQuery appendTo'); // ~15-30ms
實務建議
- 現代專案優先用原生
append/prepend/before/after:無依賴、支援多參數、效能佳
- 大量插入必用 DocumentFragment:單次重排、大幅提升效能
- jQuery 專案:
append 用於容器操作鏈,appendTo 用於新建元素即插入
- 移動現有元素:預期行為是移動,需複製時記得
.clone()/cloneNode(true)
- 避免在迴圈中重複操作 DOM:先組裝片段再一次掛載
總結
append (容器←內容) 回傳容器,appendTo (內容→容器) 回傳內容,appendChild (原生、單節點、移動語意)。現代開發推薦原生 parent.append(...nodes) 系列 API,簡潔且效能最優。