JS Selection部分中文

Selection 

Selection对象一般由window.getSelection()或其他方法返回。它代表页面中的文本选区,可能横跨多个元素。文本选区由用户拖拽鼠标经过文字而产生。 For information about text selection in an individual text editing element, see InputTextArea 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.

属性

  • anchorNode

  • 返回该选区的起点所在的节点。

  • anchorOffset

  • 返回 the number of characters that the selection's anchor is offset within the anchorNode.

  • focusNode

  • 返回该选区的终点所在的节点。

  • focusOffset

  • 返回 the number of characters that the selection's focus is offset within the focusNode.

  • isCollapsed

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

  • rangeCount

  • 返回该选区所包含多少个连续范围。

方法

  • getRangeAt

  • 返回 a range 对象 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 范围 in the selection.

  • collapseToEnd

  • Collapses the selection to the end of the last 范围 in the selection.

  • selectAllChildren

  • Adds all the children of the specified 节点 to the selection.

  • addRange

  • A range 对象 that will be added to the selection.

  • removeRange

  • Removes a 范围 from the selection.

  • removeAllRanges

  • Removes all 范围 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

  • 返回 a string currently being represented by the selection 对象, i.e. the currently selected text.

  • containsNode

  • Indicates if a certain 节点 is part of the selection.

See also


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值