outerHTML
属性在
元素
DOM 接口获取描述元素 (包括其后代) 的序列化 HTML 片段。也可以设置它为把元素替换为剖析自给定字符串的节点。
要仅获得元素内容的 HTML 表示,或替换元素内容,使用
innerHTML
特性代替。
var content = element.outerHTML; element.outerHTML = htmlString;
Reading the value of
outerHTML
返回
DOMString
containing an HTML serialization of the
element
and its descendants. Setting the value of
outerHTML
replaces the element and all of its descendants with a new DOM tree constructed by parsing the specified
htmlString
.
SyntaxError
outerHTML
using an HTML string which is not valid.
NoModificationAllowedError
outerHTML
on an element which is a direct child of a
Document
,譬如
Document.documentElement
.
获取值在元素的
outerHTML
特性:
<div id="d"> <p>Content</p> <p>Further Elaborated</p> </div>
var d = document.getElementById("d");
console.log(d.outerHTML);
// The string '<div id="d"><p>Content</p><p>Further Elaborated</p></div>'
// is written to the console window
替换节点通过设置
outerHTML
特性:
<div id="container"> <div id="d">This is a div.</div> </div>
var container = document.getElementById("container");
var d = document.getElementById("d");
console.log(container.firstChild.nodeName); // logs "DIV"
d.outerHTML = "<p>This paragraph replaced the original div.</p>";
console.log(container.firstChild.nodeName); // logs "P"
// The #d div is no longer part of the document tree,
// the new paragraph replaced it.
若元素没有父级元素,设置其
outerHTML
特性不会改变它或其后代。许多浏览器还会抛出异常。例如:
var div = document.createElement("div");
div.outerHTML = "<div class=\"test\">test</div>";
console.log(div.outerHTML); // output: "<div></div>"
Also, while the element will be replaced in the document, the variable whose
outerHTML
property was set will still hold a reference to the original element:
var p = document.getElementsByTagName("p")[0];
console.log(p.nodeName); // shows: "P"
p.outerHTML = "<div>This div replaced a paragraph.</div>";
console.log(p.nodeName); // still "P";
The returned value will contain html escaped attributes:
var anc = document.createElement("a");
anc.href = "https://developer.mozilla.org?a=b&c=d";
console.log(anc.outerHTML); // output: "<a href='https://developer.mozilla.org?a=b&c=d'></a>"
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM 剖析和序列化
The definition of 'Element.outerHTML' in that specification. |
工作草案 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
outerHTML
|
Chrome
33
|
Edge 12 | Firefox 11 | IE 4 | Opera 8 | Safari 9 |
WebView Android
4.4
|
Chrome Android
33
|
Firefox Android 14 | Opera Android 10.1 | Safari iOS 9 |
Samsung Internet Android
2.0
|
完整支持
见实现注意事项。
XMLSerializer
DOMParser
HTMLElement.outerText
元素
accessKey
属性
childElementCount
children
classList
className
clientHeight
clientLeft
clientTop
clientWidth
currentStyle
firstElementChild
id
innerHTML
lastElementChild
localName
名称
namespaceURI
nextElementSibling
onfullscreenchange
onfullscreenerror
openOrClosedShadowRoot
outerHTML
part
prefix
previousElementSibling
runtimeStyle
scrollHeight
scrollLeft
scrollLeftMax
scrollTop
scrollTopMax
scrollWidth
shadowRoot
slot
tabStop
tagName
after()
animate()
append()
attachShadow()
before()
closest()
computedStyleMap()
createShadowRoot()
getAnimations()
getAttribute()
getAttributeNames()
getAttributeNode()
getAttributeNodeNS()
getAttributeNS()
getBoundingClientRect()
getClientRects()
getElementsByClassName()
getElementsByTagName()
getElementsByTagNameNS()
hasAttribute()
hasAttributeNS()
hasAttributes()
hasPointerCapture()
insertAdjacentElement()
insertAdjacentHTML()
insertAdjacentText()
matches()
msZoomTo()
prepend()
querySelector()
querySelector()
querySelectorAll()
querySelectorAll()
releasePointerCapture()
remove()
removeAttribute()
removeAttributeNode()
removeAttributeNS()
replaceChildren()
replaceWith()
requestFullscreen()
requestPointerLock()
scroll()
scrollBy()
scrollIntoView()
scrollIntoViewIfNeeded()
scrollTo()
setAttribute()
setAttributeNode()
setAttributeNodeNS()
setAttributeNS()
setCapture()
setPointerCapture()
toggleAttribute()
afterscriptexecute
auxclick
blur
click
compositionend
compositionstart
compositionupdate
contextmenu
copy
cut
dblclick
DOMActivate
DOMMouseScroll
error
focus
focusin
focusout
fullscreenchange
fullscreenerror
gesturechange
gestureend
gesturestart
keydown
keypress
keyup
mousedown
mouseenter
mouseleave
mousemove
mouseout
mouseover
mouseup
mousewheel
MozMousePixelScroll
msContentZoom
MSGestureChange
MSGestureEnd
MSGestureHold
MSGestureStart
MSGestureTap
MSInertiaStart
MSManipulationStateChanged
overflow
paste
scroll
select
show
touchcancel
touchend
touchmove
touchstart
underflow
webkitmouseforcechanged
webkitmouseforcedown
webkitmouseforceup
webkitmouseforcewillbegin
wheel
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