insertAdjacentElement()
方法在
元素
interface inserts a given element node at a given position relative to the element it is invoked upon.
targetElement.insertAdjacentElement(position, element);
位置
DOMString
representing the position relative to the
targetElement
; must match (case-insensitively) one of the following strings:
'beforebegin'
: Before the
targetElement
本身。
'afterbegin'
: Just inside the
targetElement
, before its first child.
'beforeend'
: Just inside the
targetElement
, after its last child.
'afterend'
: After the
targetElement
本身。
element
The element to be inserted into the tree.
The element that was inserted, or
null
, if the insertion failed.
| 异常 | 解释 |
|---|---|
SyntaxError
|
位置
specified is not a recognised value.
|
TypeError
|
element
specified is not a valid element.
|
<!--注意:beforebegin--><p><!--afterbegin--> foo <!--beforeend--></p><!--afterend-->
beforebegin
and
afterend
positions work only if the node is in a tree and has an element parent.
beforeBtn.addEventListener('click', function() {
var tempDiv = document.createElement('div');
tempDiv.style.backgroundColor = randomColor();
if (activeElem) {
activeElem.insertAdjacentElement('beforebegin',tempDiv);
}
setListener(tempDiv);
});
afterBtn.addEventListener('click', function() {
var tempDiv = document.createElement('div');
tempDiv.style.backgroundColor = randomColor();
if (activeElem) {
activeElem.insertAdjacentElement('afterend',tempDiv);
}
setListener(tempDiv);
});
Have a look at our
insertAdjacentElement.html
demo on Github (see the
源代码
too.) Here we have a sequence of
<div>
elements inside a container. When one is clicked, it becomes selected and you can then press the
Insert before
and
Insert after
buttons to insert new divs before or after the selected element using
insertAdjacentElement()
.
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'insertAdjacentElement()' in that specification. |
实时标准 |
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
insertAdjacentElement
|
Chrome 1 |
Edge
18
|
Firefox 48 |
IE
8
|
Opera 8 | Safari 10 | WebView Android 1 | Chrome Android 18 | Firefox Android 48 | Opera Android 10.1 | Safari iOS 10 | Samsung Internet Android 1.0 |
完整支持
见实现注意事项。
Element.insertAdjacentHTML()
Element.insertAdjacentText()
Node.insertBefore()
(类似
beforebegin
, with different arguments)
Node.appendChild()
(same effect as
beforeend
)
元素
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