JS Selection

DOM Selection 方法详解

document.getSelection

IN THIS ARTICLE

The DOM getSelection() method is available on the Window and Document interfaces.
See window.getSelection() for details.


getSelection函数在Window上Document的接口上都是有效的



This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for the proper prefixes to use in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the spec changes.

Introduction

Selection is the class of the object returned by window.getSelection() and other methods. It represents the text selection in the greater page, possibly spanning multiple elements, when the user drags over static text and other parts of the page. For information about text selection in an individual text editing element, see InputTextArea and document.activeElement which typically return the parent object returned from window.getSelection().

A selection object represents the ranges that the user has selected. Typically, it holds only one range, accessed as follows:

var selObj = window.getSelection();var range  = selObj.getRangeAt(0);

  • selObj is a Selection object

  • range is a Range object

Calling the Selection/toString() method returns the text contained in the selection, e.g

var selObj = window.getSelection();window.alert(selObj);

Note that using a selection object as the argument to window.alert will call the object's toStringmethod.

Glossary

Other key terms used in this section.

  • anchor

  • The anchor of a selection is the beginning point of the selection. When making a selection with a mouse, the anchor is where in the document the mouse button is initially pressed. As the user changes the selection using the mouse or the keyboard, the anchor does not move.

  • focus

  • The focus of a selection is the end point of the selection. When making a selection with a mouse, the focus is where in the document the mouse button is released. As the user changes the selection using the mouse or the keyboard, the focus is the end of the selection that moves.

  • range

  • A range is a contiguous part of a document. A range can contain entire nodes as well as portions of nodes, such as a portion of a text node. A user will normally only select a single range at a time, but it's possible for a user to select multiple ranges (e.g. by using the Control key). A range can be retrieved from a selection as a range object. Range objects can also be created via the DOM and programmatically added or removed from a selection.

Properties

  • anchorNode

  • Returns the Node in which the selection begins.

  • anchorOffset

  • Returns a number representing the offset of the selection's anchor within the anchorNode. If anchorNode is a text node, this is the number of characters within anchorNode preceding the anchor. If anchorNode is an element, this is the number of child nodes of the anchorNode preceding the anchor.

  • focusNode

  • Returns the Node in which the selection ends.

  • focusOffset

  • Returns a number representing the offset of the selection's anchor within the focusNode. If focusNode is a text node, this is the number of characters within focusNode preceding the focus. If focusNode is an element, this is the number of child nodes of the focusNode preceding the focus.

  • isCollapsed

  • Returns a Boolean indicating whether the selection's start and end points are at the same position.

  • rangeCount

  • Returns the number of ranges in the selection.

Methods

  • getRangeAt

  • Returns a Range object representing one of the ranges currently selected.

  • collapse

  • Collapses the current selection to a single point.

  • extend

  • Moves the focus of the selection to a specified point.

  • modify

  • Changes the current selection.

  • collapseToStart

  • Collapses the selection to the start of the first range in the selection.

  • collapseToEnd

  • Collapses the selection to the end of the last range in the selection.

  • selectAllChildren

  • Adds all the children of the specified node to the selection.

  • addRange

  • Range object that will be added to the selection.

  • removeRange

  • Removes a range from the selection.

  • removeAllRanges

  • Removes all ranges from the selection.

  • deleteFromDocument

  • Deletes the selection's content from the document.

  • selectionLanguageChange

  • Modifies the cursor Bidi level after a change in keyboard direction.

  • toString

  • Returns a string currently being represented by the selection object, i.e. the currently selected text.

  • containsNode

  • Indicates if a certain node is part of the selection.

See also


转载于:https://my.oschina.net/122612475/blog/285857

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值