CharacterData: after() method

فهرست فصل‌ها دانلود آرشیوناشر

MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونه‌کد

{{APIRef("DOM")}}

The **after()** method of the {{domxref("CharacterData")}} interface inserts a set of {{domxref("Node")}} objects or strings in the children list of the CharacterData's parent, just after the CharacterData. Strings are inserted as equivalent {{domxref("Text")}} nodes. Nothing happens if {{domxref("Node.parentNode", "parentNode")}} is null.

Syntax

js-nolint
after(param1)
after(param1, param2)
after(param1, param2, /* …, */ paramN)

Parameters

  • param1, …, paramN
  • : A set of {{domxref("Node")}} objects or strings to insert.

Return value

None ({{jsxref("undefined")}}).

Exceptions

  • HierarchyRequestError {{DOMxRef("DOMException")}}
  • : Thrown when the new nodes cannot be inserted at the specified point in the hierarchy,

that is if one of the following conditions is met:

  • If the insertion of one of the added node would lead to a cycle, that is if one of them is an ancestor of this {{domxref("CharacterData")}} node.
  • If one of the added node is not a {{domxref("DocumentFragment")}}, a {{domxref("DocumentType")}}, an {{domxref("Element")}}, or a {{domxref("CharacterData")}}.
  • If this {{domxref("CharacterData")}} node is actually a {{domxref("Text")}} node, and its parent is a {{domxref("Document")}}.
  • If the parent of this {{domxref("CharacterData")}} node is a {{domxref("Document")}} and one of the nodes to insert is a {{domxref("DocumentFragment")}} with more than one {{domxref("Element")}} child, or that has a {{domxref("Text")}} child.

Examples

The after() method allows you to insert new nodes after a CharacterData node.

js
const h1TextNode = document.querySelector("h1").firstChild;
h1TextNode.after(" #h1");

h1TextNode.parentElement.childNodes;
// NodeList [#text "CharacterData.after()", #text " #h1"]

h1TextNode.data;
// "CharacterData.after()"

توجه: If you rather want to append text to the current node, the [appendData()](/en-US/docs/Web/API/CharacterData/appendData) method lets you append to the current node's data.

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • {{domxref("CharacterData.appendData()")}}
  • {{domxref("CharacterData.before()")}}
  • {{domxref("DocumentType.after()")}}
  • {{domxref("Element.after()")}}
  • {{domxref("Element.append()")}}
  • {{domxref("Node.appendChild()")}}
  • {{domxref("Element.insertAdjacentElement()")}}