Range.setStart() - Web APIs 编辑

The Range.setStart() method sets the start position of a Range.

If the startNode is a Node of type Text, Comment, or CDataSection, then startOffset is the number of characters from the start of startNode. For other Node types, startOffset is the number of child nodes between the start of the startNode.

Setting the start point below (lower in the document) the end point will result in a collapsed range with the start and end points both set to the specified start position.

Syntax

range.setStart(startNode, startOffset);

Parameters

startNode
The Node where the Range should start.
startOffset
An integer greater than or equal to zero representing the offset for the start of the Range from the start of startNode.

Examples

Highlight part of an element

This example uses the Range.setStart() and Range.setEnd() methods to add part of an address to a range. The selected range is then highlighted using  Range.surroundContents().

The address contains nine nodes: five text nodes, and four <br> elements.

HTML

<p id="address">Wyatt Earp<br>
101 E. Main St.<br>
Dodge City, KS<br>
67801<br>
USA</p>

<hr>
<p>Nodes in the original address:</p>
<ol id="log"></ol>

JavaScript

const address = document.getElementById('address');
const log = document.getElementById('log');

// Log info
address.childNodes.forEach(node => {
  const li = document.createElement('li');
  li.textContent = `${node.nodeName}, ${node.nodeValue}`;
  log.appendChild(li);
});

// Highlight the street and city
const startOffset = 2;  // Start at third node: 101 E. Main St.
const endOffset = 5;    // End at fifth node: Dodge City, KS
const range = document.createRange();
range.setStart(address, startOffset);
range.setEnd(address, endOffset);

const mark = document.createElement('mark');
range.surroundContents(mark);

Result

Get characters from a text node

This example uses the Range.setStart() and Range.setEnd() methods to define the contents of a range. The resulting range contains the first through fifth characters within a text node.

HTML

<p id="content">0123456789</p>
<p id="log"></p>

JavaScript

const element = document.getElementById('content');
const textNode = element.childNodes[0];
const range = document.createRange();
range.setStart(textNode, 0);  // Start at first character
range.setEnd(textNode, 5);    // End at fifth character
document.getElementById('log').textContent = range;

Result

Specifications

SpecificationStatusComment
DOM
The definition of 'Range.setStart()' in that specification.
Living StandardNo change.
Document Object Model (DOM) Level 2 Traversal and Range Specification
The definition of 'Range.setStart()' in that specification.
ObsoleteInitial specification.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:84 次

字数:6386

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文