insertAdjacentText()
方法在
元素
interface inserts a given text node at a given position relative to the element it is invoked upon.
element.insertAdjacentText(position, element);
位置
DOMString
representing the position relative to the
element
; must be one of the following strings:
'beforebegin'
: Before the
element
本身。
'afterbegin'
: Just inside the
element
, before its first child.
'beforeend'
: Just inside the
element
, after its last child.
'afterend'
: After the
element
本身。
element
DOMString
representing the text to be inserted into the tree.
Void.
| 异常 | 解释 |
|---|---|
SyntaxError
|
位置
specified is not a recognised value.
|
<!--注意: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() {
para.insertAdjacentText('afterbegin',textInput.value);
});
afterBtn.addEventListener('click', function() {
para.insertAdjacentText('beforeend',textInput.value);
});
Have a look at our
insertAdjacentText.html
demo on GitHub (see the
源代码
too.) Here we have a simple paragraph. You can enter some text into the form element, then press the
Insert before
and
Insert after
buttons to insert it before or after the existing paragraph text using
insertAdjacentText()
. Note that the existing text node is not added to — further text nodes are created containing the new additions.
You can polyfill the insertAdjacentText
() method
in Internet Explorer 5.5 (maybe earlier) and higher with the following code:
if (!Element.prototype.insertAdjacentText)
Element.prototype.insertAdjacentText = function(type, txt){
this.insertAdjacentHTML(
type,
(txt+'') // convert to string
.replace(/&/g, '&') // embed ampersand symbols
.replace(/</g, '<') // embed less-than symbols
)
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'insertAdjacentText()' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
insertAdjacentText
|
Chrome 1 |
Edge
18
|
Firefox 48 |
IE
Yes
|
Opera Yes | Safari 10 | WebView Android 2.3 | Chrome Android 18 | Firefox Android 48 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android 1.0 |
完整支持
见实现注意事项。
元素
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