1、getRangeAt(index):从当前selection对象中获得一个range对象。
index:参考rangeCount属性。
返回:根据下标index返回相应的range对象。
2、collapse(parentNode, offset):将开始点和结束点合并到指定节点(parentNode)的相应(offset)位置。
parentNode:焦点(插入符)将会在此节点内。
offset: 取值范围应当是[0, 1, 2, 3, parentNode.childNodes.length]。
0:定位到第一个子节点前。
1:第二个子节点前。
……
childNodes.length-1:最后一个子节点前。
childNodes.length:最后一个子节点后。
Mozilla官方文档 中讲到取值为0和1,经测试不准确。文档中还有一句不是十分清楚“The document is not modified. If the content is focused and editable, the caret will blink there.”
3:extend(parentNode, offset)
将“结束点”移动到指定节点(parentNode)的指定位置(offset)。
“起点”不会移动,新的selection是从“起点”到“结束点”的区域,与方向无关(新的“结束点”可以在原“起点”的前面)。
parentNode:焦点将会在此节点内。
Offset:1,parentNode的最后;0,parentNode的最前。
4、modify(alter, direction, granularity)
改变焦点的位置,或扩展|缩小selection的大小
alter:改变的方式。”move”,用于移动焦点;”extend”,用于改变selection。
direction:移动的方向。可选值forward | backword或left | right
granularity:移动的单位或尺寸。可选值,character", "word", "sentence", "line", "paragraph", "lineboundary", "sentenceboundary", "paragraphboundary", or "documentboundary"。
Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1才会支持此函数,
官方文档:https://developer.mozilla.org/en/DOM/Selection/modify
5、collapseToStart():将“结束点”移动到,selction的“起点”,多个range时也是如此。
6、collapseToEnd():将“起点”移动到,selction的“结束点”,多个range时也是如此。
7、selectAllChildren(parentNode):将parentNode的所有后代节点(parentNode除外)变为selection,页面中原来的selection将被抛弃。
8、apRange(range)
将range添加到selection当中,所以一个selection中可以有多个range。
注意Chrome不允许同时存在多个range,它的处理方式和Firefox有些不同。
9、removeRange(range)
从当前selection移除range对象,返回值undefined。
Chrome目前没有改函数,即便是在Firefox中如果用自己创建(document.createRange())的range作为参数也会报错。
如果用oSelction.getRangeAt()取到的,则不会报错。
10、removeAllRanges():移除selection中所有的range对象,执行后anchorNode、focusNode被设置为null,不存在任何被选中的内容。
11、toString():返回selection的纯文本,不包含标签。
12、containsNode(aNode, aPartlyContained)
判断一个节点是否是selction的一部分。
aNode:要验证的节点。
aPartlyContained:true,只要aNode有一部分属于selection就返回true;false,aNode必须全部属于selection时才返回true。