Range.extractContents()
method moves contents of the
范围
from the document tree into a
DocumentFragment
.
Event listeners added using DOM Events are not retained during extraction. HTML attribute events are retained or duplicated as they are for the
Node.cloneNode()
method. HTML
id
attributes are also cloned, which can lead to an invalid document if a partially-selected node is extracted and appended to the document.
Partially selected nodes are cloned to include the parent tags necessary to make the document fragment valid.
documentFragment = range.extractContents();
var range = document.createRange();
range.selectNode(document.getElementsByTagName("div").item(0));
var documentFragment = range.extractContents();
document.body.appendChild(documentFragment);
This example lets you move items between two containers. Select one or more item, and then click "swap" to move them to the opposite container.
<p id="list1">123456</p> <button id="swap">Swap selected item(s)</button> <p id="list2">abcdef</p>
body {
pointer-events: none;
}
p {
border: 1px solid;
font-size: 2em;
padding: .3em;
}
button {
font-size: 1.2em;
padding: .5em;
pointer-events: auto;
}
const list1 = document.getElementById('list1');
const list2 = document.getElementById('list2');
const button = document.getElementById('swap');
button.addEventListener('click', e => {
selection = window.getSelection();
for (let i = 0; i < selection.rangeCount; i++) {
const range = selection.getRangeAt(i);
if (range.commonAncestorContainer === list1 ||
range.commonAncestorContainer.parentNode === list1) {
const documentFragment = range.extractContents();
list2.appendChild(documentFragment);
} else if (range.commonAncestorContainer === list2 ||
range.commonAncestorContainer.parentNode === list2) {
const documentFragment = range.extractContents();
list1.appendChild(documentFragment);
}
}
});
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
extractContents
|
Chrome Yes | Edge 12 | Firefox 4 | IE 9 | Opera 9 | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 4 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
范围
cloneContents()
cloneRange()
collapse()
compareBoundaryPoints()
compareNode()
comparePoint()
createContextualFragment()
deleteContents()
detach()
extractContents()
getBoundingClientRect()
getClientRects()
insertNode()
intersectsNode()
isPointInRange()
selectNode()
selectNodeContents()
setEnd()
setEndAfter()
setEndBefore()
setStart()
setStartAfter()
setStartBefore()
surroundContents()
toString()
AbortController
AbortSignal
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
CustomEvent
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMTokenList
DOMUserData
Document
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
节点
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument