Text.splitText() method breaks the 文本 node into two nodes at the specified offset, keeping both nodes in the tree as siblings.

After the split, the current node contains all the content up to the specified offset point, and a newly created node of the same type contains the remaining text. The newly created node is returned to the caller. If the original node had a parent, the new node is inserted as the next sibling of the original node. If the offset is equal to the length of the original node, the newly created node has no data.

Separated text nodes can be concatenated using the Node.normalize() 方法。

句法

newNode = textNode.splitText(offset)
					

参数

offset

The index immediately before which to break the text node.

返回值

返回新近创建的 文本 node that contains the text after the specified offset point.

Exceptions thrown

A DOMException 采用值 INDEX_SIZE_ERR is thrown if the specified offset is negative or is greater than the number of 16-bit units in the node's text; a DOMException 采用值 NO_MODIFICATION_ALLOWED_ERR is thrown if the node is read-only.

范例

In this example, the text of a <p> is split into two text nodes, and a <u> is inserted between them.

HTML

<p>foobar</p>
					

JavaScript

const p = document.querySelector('p');
// Get contents of <p> as a text node
const foobar = p.firstChild;
// Split 'foobar' into two text nodes, 'foo' and 'bar',
// and save 'bar' as a const
const bar = foobar.splitText(3);
// Create a <u> element containing ' new content '
const u = document.createElement('u');
u.appendChild(document.createTextNode(' new content '));
// Add <u> before 'bar'
p.insertBefore(u, bar);
// The result is: <p>foo<u> new content </u>bar</p>
					

结果

规范

规范 状态 注释
DOM
The definition of 'Text.splitText' in that specification.
实时标准 无变化自 DOM (文档对象模型) 3 级核心规范 .
DOM (文档对象模型) 3 级核心规范
The definition of 'Text.splitText' in that specification.
过时 无变化自 DOM (文档对象模型) 级别 2 核心规范 .
DOM (文档对象模型) 级别 2 核心规范
The definition of 'Text.splitText' in that specification.
过时 无变化自 DOM (文档对象模型) 1 级规范 .
DOM (文档对象模型) 1 级规范
The definition of 'Text.splitText' 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 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
splitText Chrome 1
1
Before Chrome 30, the offset argument was optional.
Edge 12 Firefox 1 IE Yes Opera Yes
Yes
Before Opera 17, the offset argument was optional.
Safari Yes
Yes
offset argument is optional.
WebView Android Yes
Yes
Before version 4.4, the offset argument was optional.
Chrome Android 18
18
Before Chrome 30, the offset argument was optional.
Firefox Android 4 Opera Android Yes
Yes
Before Opera 17, the offset argument was optional.
Safari iOS Yes
Yes
offset argument is optional.
Samsung Internet Android 1.0
1.0
Before Samsung Internet 2.0, the offset argument was optional.

图例

完整支持

完整支持

见实现注意事项。

另请参阅

元数据

  • 最后修改: