DOM:Selection
Redirected from en/DOM:Selection
Selection
Table of contents
- 1. Introduction
- 2. Glossary
- 3. Properties
- 4. Methods
- 5. See also
- 6. External links
Introduction
Selection is the class of the object returned by window.getSelection()
and other methods. A selection object represents the ranges that the user has selected. Typically, it holds only one range, accessed as follows:
range = sel.getRangeAt(0);
Calling the toString()
method returns the text contained in the selection, e.g
selObj = window.getSelection(); window.alert(selObj);
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 the number of characters that the selection's anchor is offset within the anchorNode.
- focusNode
- Returns the node in which the selection ends.
- focusOffset
- Returns the number of characters that the selection's focus is offset within the focusNode.
- 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.
- collapseToStart
- Moves the focus of the selection to the same point at the anchor.
- collapseToEnd
- Moves the anchor of the selection to the same point as the focus. The focus does not move.
- selectAllChildren
- Adds all the children of the specified node to the selection.
- addRange
- A 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
- 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
Page last modified 23:18, 5 May 2008 by Jay I.?
加支付宝好友偷能量挖...