--- title: Range.insertNode() slug: Web/API/Range/insertNode translation_of: Web/API/Range/insertNode ---
{{ApiRef("DOM")}}

Range.insertNode() 是在{{domxref("Range")}}的起始位置插入节点的方法。

新节点是插入在 the Range起始位置。如果将新节点添加到一个文本 {{domxref("节点")}}, 则该节点在插入点处被拆分,插入发生在两个文本节点之间

如果新节点是一个文档片段,则插入文档片段的子节点。

Syntax

range.insertNode(newNode);

Parameters

newNode
The {{domxref("Node")}} to insert at the start of the range.

Example

range = document.createRange();
newNode = document.createElement("p");
newNode.appendChild(document.createTextNode("New Node Inserted Here"));
range.selectNode(document.getElementsByTagName("div").item(0));
range.insertNode(newNode);

Specifications

Specification Status Comment
{{SpecName('DOM WHATWG', '#dom-range-insertnode', 'Range.insertNode()')}} {{Spec2('DOM WHATWG')}} No change
{{SpecName('DOM2 Traversal_Range', 'ranges.html#Level2-Range-method-insertNode', 'Range.insertNode()')}} {{Spec2('DOM2 Traversal_Range')}} Initial definition

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatGeckoDesktop("1.0")}}
{{CompatGeckoDesktop("14.0")}}[1]
9.0 9.0 {{CompatVersionUnknown}}
Feature Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatGeckoMobile("1.0")}}
{{CompatGeckoDesktop("14.0")}}[1]
9.0 9.0 {{CompatVersionUnknown}}

[1] Prior to Gecko 14.0 {{geckoRelease("14.0")}}, this method had no effect on collapsed ranges. Now it behaves as per the specification.

See also