aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/api/documentfragment/index.html
blob: 07236fec4374c1c30f00e07c7b6f6c5cfb62f9f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
---
title: DocumentFragment
slug: Web/API/DocumentFragment
translation_of: Web/API/DocumentFragment
---
<p>{{ ApiRef("DOM") }}</p>

<p><strong><span class="external"><code>DocumentFragment</code></span></strong> 介面表示了一個沒有父節點的最小化文件物件。<code>DocumentFragment</code> 被當作一種輕量化的 {{domxref("Document")}},用如同標準文件一般的方式保存片段的文件結構(由節點組成)。關鍵的區別在於文件片段不是真實的 DOM 結構,文件片段的變動並不會影響目前的網頁文件,也不會導致回流({{Glossary("reflow")}})或引起任何影響效能的情況發生。</p>

<p>一般的用法是建立一個 <code>DocumentFragment</code> 物件,在此物件中組織一個 DOM 的子樹。再使用 {{domxref("Node")}} 介面定義的方法,如 {{domxref("Node.appendChild", "appendChild()")}}{{domxref("Node.insertBefore", "insertBefore()")}} 將這個文件片段加入或插入目前頁面的 DOM 當中。執行這個將文件片段中的節點置入 DOM 的動作之後,會留下一個空的 <code>DocumentFragment</code> 物件(只會插入物件中的節點,<code>DocumentFragment</code> 物件本身不會被插入)。由於文件片段中的所有節點是一次性的被插入目前頁面文件當中,故回流及頁面渲染只會被觸發一次,所以可用插入 <code>DocumentFragment</code> 物件的方式取代傳統分別插入多個節點至 DOM(將節點一個一個分次進行插入)的操作方式。</p>

<p>此介面也適合與 Web components 搭配使用:{{HTMLElement("template")}} 元素在其 {{domxref("HTMLTemplateElement.content")}} 屬性中便包含了一個 <code>DocumentFragment</code> 物件。</p>

<p>可使用 {{domxref("document.createDocumentFragment()")}} 方法或 <code>DocumentFragment</code> 的建構式來建立一個空的 <code>DocumentFragment</code> 物件。</p>

<h2 id="Specification" name="Specification">屬性</h2>

<p><em>This interface has no specific properties, but inherits those of its parent, </em><em>{{domxref("Node")}}, and implements those of the {{domxref("ParentNode")}} interface.</em></p>

<dl>
 <dt>{{ domxref("ParentNode.children") }} {{readonlyInline}}{{experimental_inline}}</dt>
 <dd>Returns a live {{domxref("HTMLCollection")}} containing all objects of type {{domxref("Element")}} that are children of the <code>DocumentFragment</code> object.</dd>
 <dt>{{ domxref("ParentNode.firstElementChild") }} {{readonlyInline}}{{experimental_inline}}</dt>
 <dd>Returns the {{domxref("Element")}} that is the first child of the <code>DocumentFragment</code> object, or <code>null</code> if there is none.</dd>
 <dt>{{ domxref("ParentNode.lastElementChild") }} {{readonlyInline}}{{experimental_inline}}</dt>
 <dd>Returns the {{domxref("Element")}} that is the last child of the <code>DocumentFragment</code> object, or <code>null</code> if there is none.</dd>
 <dt>{{ domxref("ParentNode.childElementCount") }} {{readonlyInline}}{{experimental_inline}}</dt>
 <dd>Returns an <code>unsigned long</code> giving the amount of children that the <code>DocumentFragment</code> has.</dd>
</dl>

<h2 id="建構式">建構式</h2>

<dl>
 <dt>{{ domxref("DocumentFragment.DocumentFragment()", "DocumentFragment()") }} {{experimental_inline}}</dt>
 <dd>Returns an empty <code>DocumentFragment</code> object.</dd>
</dl>

<h2 id="方法">方法</h2>

<p><em>This interface inherits the methods of its parent, {{domxref("Node")}}<em>, and implements those of the {{domxref("ParentNode")}} interface</em></em><em>.</em></p>

<dl>
 <dt>{{domxref("DocumentFragment.find()")}} {{experimental_inline}}</dt>
 <dd>Returns the first matching {{domxref("Element")}} in the tree of the <code>DocumentFragment</code>.</dd>
 <dt>{{domxref("DocumentFragment.findAll()")}} {{experimental_inline}}</dt>
 <dd>Returns a {{domxref("NodeList")}} of matching {{domxref("Element")}} in the tree of the <code>DocumentFragment</code>.</dd>
 <dt>{{domxref("DocumentFragment.querySelector()")}}</dt>
 <dd>Returns the first {{domxref("Element")}} node within the <code>DocumentFragment</code>, in document order, that matches the specified selectors.</dd>
 <dt>{{domxref("DocumentFragment.querySelectorAll()")}}</dt>
 <dd>Returns a {{domxref("NodeList")}} of all the {{domxref("Element")}} nodes within the <code>DocumentFragment</code> that match the specified selectors.</dd>
</dl>

<dl>
 <dt>{{domxref("DocumentFragment.getElementById()")}}</dt>
 <dd>Returns the first {{domxref("Element")}} node within the <code style="font-size: 14px;">DocumentFragment</code>, in document order, that matches the specified ID.</dd>
</dl>

<h2 id="Specifications" name="Specifications">規範</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('DOM WHATWG', '#interface-documentfragment', 'DocumentFragment')}}</td>
   <td>{{Spec2('DOM WHATWG')}}</td>
   <td>Added the constructor and the implementation of {{domxref("ParentNode")}}.</td>
  </tr>
  <tr>
   <td>{{SpecName('Selectors API Level 2', '#the-apis', 'DocumentFragment')}}</td>
   <td>{{Spec2('Selectors API Level 2')}}</td>
   <td>Added the <code>find()</code> and <code>findAll()</code> methods.</td>
  </tr>
  <tr>
   <td>{{SpecName('Selectors API Level 1', '#the-apis', 'DocumentFragment')}}</td>
   <td>{{Spec2('Selectors API Level 1')}}</td>
   <td>Added the <code>querySelector()</code> and <code>querySelectorAll()</code> methods.</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM3 Core', 'core.html#ID-B63ED1A3', 'DocumentFragment')}}</td>
   <td>{{Spec2('DOM3 Core')}}</td>
   <td>No change from {{SpecName('DOM2 Core')}}</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM2 Core', 'core.html#ID-B63ED1A3', 'DocumentFragment')}}</td>
   <td>{{Spec2('DOM2 Core')}}</td>
   <td>No change from {{SpecName('DOM1')}}</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM1', 'level-one-core.html#ID-B63ED1A3', 'DocumentFragment')}}</td>
   <td>{{Spec2('DOM1')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="瀏覽器相容性">瀏覽器相容性</h2>

{{Compat("api.DocumentFragment")}}

<h2 id="參見">參見</h2>

<ul>
 <li><a href="/docs/DOM/DOM_Reference" title="/en-US/docs/DOM/DOM_Reference">The DOM interfaces index.</a></li>
</ul>