Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Element.insertAdjacentText()

我们的志愿者还没有将这篇文章翻译为 中文 (简体)加入我们帮助完成翻译!

The insertAdjacentText() method inserts a given text node at a given position relative to the element it is invoked upon.

Syntax

element.insertAdjacentText(position, element);

Parameters

position
A DOMString representing the position relative to the element; must be one of the following strings:
  • 'beforebegin': Before the element itself.
  • 'afterbegin': Just inside the element, before its first child.
  • 'beforeend': Just inside the element, after its last child.
  • 'afterend': After the element itself.
element
A DOMString representing the text to be inserted into the tree.

Return value

Void.

Exceptions

Exception Explanation
SyntaxError The position specified is not a recognised value.

Visualization of position names

<!-- beforebegin -->
<p>
<!-- afterbegin -->
foo
<!-- beforeend -->
</p>
<!-- afterend -->
Note: The beforebegin and afterend positions work only if the node is in a tree and has an element parent.

Example

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 source code 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.

Specification

Specification Status Comment
DOM
The definition of 'insertAdjacentText()' in that specification.
Living Standard  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (Yes) 48.0 (48.0) ? (Yes) (Yes)
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support ? 48.0 (48.0) ? ? ?

See also

文档标签和贡献者

 此页面的贡献者: Marat-Tanalin, vsemozhetbyt, chrisdavidmills
 最后编辑者: Marat-Tanalin,