Selection
Selection对象一般由window.getSelection()
或其他方法返回。它代表页面中的文本选区,可能横跨多个元素。文本选区由用户拖拽鼠标经过文字而产生。 For information about text selection in an individual text editing element, see Input
, TextArea
and document.activeElement
which typically 返回 the parent 对象 returned from window.getSelection()
.
A selection 对象 represents the ranges
that the 用户 has selected. Typically, it holds only one 范围, accessed as follows:
var selObj = window.getSelection();var range = selObj.getRangeAt(0);
selObj
表示 Selection 对象range
表示Range 对象
Calling the Selection/toString()
方法 返回 the text contained in the selection, e.g
var selObj = window.getSelection();window.alert(selObj);
Note that using a selection 对象 as the argument to window.alert
will call the 对象's toString
方法.
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 用户 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 用户 changes the selection using the mouse or the keyboard, the focus is the end of the selection that moves.
range
A 范围 is a contiguous part of a document. A 范围 can contain entire 节点 as well as portions of 节点, such as a portion of a text 节点. A 用户 will normally only select a single 范围 at a time, but it's possible for a 用户 to select multiple 范围 (e.g. by using the Control key). A 范围 can be retrieved from a selection as a
range
对象. Range 对象 can also be created via the DOM and programmatically added or removed from a selection.
属性
返回该选区的起点所在的节点。
返回 the number of characters that the selection's anchor is offset within the anchorNode.
返回该选区的终点所在的节点。
返回 the number of characters that the selection's focus is offset within the focusNode.
返回 a Boolean indicating whether the selection's start and end points are at the same position.
返回该选区所包含多少个连续范围。
方法
返回 a range 对象 representing one of the ranges currently selected.
Collapses the current selection to a single point.
Moves the focus of the selection to a specified point.
Changes the current selection.
Collapses the selection to the start of the first 范围 in the selection.
Collapses the selection to the end of the last 范围 in the selection.
Adds all the children of the specified 节点 to the selection.
A range 对象 that will be added to the selection.
Removes a 范围 from the selection.
Removes all 范围 from the selection.
Deletes the selection's content from the document.
Modifies the cursor Bidi level after a change in keyboard direction.
返回 a string currently being represented by the selection 对象, i.e. the currently selected text.
Indicates if a certain 节点 is part of the selection.
See also
HTML5 DOM Range Interface Selection