简介
selection是对当前激活选中区(即高亮文本)进行操做。javascript
在非IE浏览器(Firefox、Safari、Chrome、Opera)下可使用window.getSelection()得到selection对象,本文讲述的是标准的selection操做方法。文中绝大部份内容来自 https://developer.mozilla.org/en/DOM/Selectionhtml
术语
如下几个名词是英文文档中的几个名词。java
-
anchor
-
选中区域的“起点”。
-
focus
-
选中区域的“结束点”。
-
range
-
是一种fragment(HTML片段),它包含了节点或文本节点的一部分。通常状况下,同一时刻页面中只可能有一个range,也有多是多个range(使用Ctrl健进行多选,不过有的浏览器不容许,例如Chrome)。能够从selection中得到range对象,也可使用document.createRange()方法得到。
属性
-
anchorNode
-
返回包含“起点”的节点。
-
anchorOffset
-
“起点”在anchorNode中的偏移量。
-
focusNode
-
返回包含“结束点”的节点。
-
focusOffset
-
“结束点”在focusNode中的偏移量。
-
isCollapsed
-
“起点”和“结束点”是否重合。
-
rangeCount
-
返回selection中包含的range对象的数目,通常存在一个range,Ctrl健配合使用能够有多个。
方法
-
getRangeAt(index)
-
从当前selection对象中得到一个range对象。
index:参考rangeCount属性。
返回:根据下标index返回相应的range对象。
-
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.”
-
extend(parentNode, offset)
-
将“结束点”移动到指定节点(parentNode)的指定位置(offset)。
“起点”不会移动,新的selection是从“起点”到“结束点”的区域,与方向无关(新的“结束点”能够在原“起点”的前面)。
parentNode:焦点将会在此节点内。
Offset:1,parentNode的最后;0,parentNode的最前。
-
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
-
collapseToStart()
-
将“结束点”移动到,selction的“起点”,多个range时也是如此。
-
collapseToEnd()
-
将“起点”移动到,selction的“结束点”,多个range时也是如此。
-
selectAllChildren(parentNode)
-
将parentNode的全部后代节点(parentNode除外)变为selection,页面中原来的selection将被抛弃。
-
addRange(range)
-
将range添加到selection当中,因此一个selection中能够有多个range。
注意Chrome不容许同时存在多个range,它的处理方式和Firefox有些不一样。
-
removeRange(range)
-
从当前selection移除range对象,返回值undefined。
Chrome目前没有改函数,即使是在Firefox中若是用本身建立(document.createRange())的range做为参数也会报错。
若是用oSelction.getRangeAt()取到的,则不会报错。
-
removeAllRanges()
-
移除selection中全部的range对象,执行后anchorNode、focusNode被设置为null,不存在任何被选中的内容。
-
toString()
-
返回selection的纯文本,不包含标签。
-
containsNode(aNode, aPartlyContained)
-
判断一个节点是不是selction的一部分。
aNode:要验证的节点。
aPartlyContained:true,只要aNode有一部分属于selection就返回true;false,aNode必须所有属于selection时才返回true。
document.activeElement
该属性属于HTML5中的一部分,它返回当前得到焦点的元素,若是不存在则返回“body”。通常状况下返回的是“文本域”或“文本框”。也有可能返回“下拉列表”、“按钮”、“单选”或“多选按钮”等等,Mac OS系统中可能不会返回非输入性元素(textbox,例如文本框、文本域)。浏览器
相关属性和方法:函数
-
selectionStart
-
输入性元素selection起点的位置,可读写。
-
selectionEnd
-
输入性元素selection结束点的位置,可读写。
-
setSelectionRange(start, end)
-
设置输入性元素selectionStart和selectionEnd值
- 若是textbox没有selection时,selectionStart和selectionEnd相等,都是焦点的位置。
- 在使用setSelectionRange()时
若是end小于start,会讲selectionStart和selectionEnd都设置为end;
若是start和end参数大于textbox内容的长度(textbox.value.length),start和end都会设置为value属性的长度。
- 值得一提的是selectionStart和selectionEnd会记录元素最后一次selection的相关属性,意思就是当元素失去焦点后,使用selectionStart和selectionEnd仍可以获取到元素失去焦点时的值。这个特性可能会对制做“插入表情”时十分有用(将表情插入到元素最后一次焦点的位置)。
<textarea id="textbox">abc中国efg</textarea>
<script type="text/javascript">
window.onload = function(){
var textbox = document.getElementById('textbox');
textbox.setSelectionRange(5, 2);
console.log(textbox.selectionStart); // 2
console.log(textbox.selectionEnd); // 2
};
</script>
<textarea id="textbox">abc中国efg</textarea>
<script type="text/javascript">
window.onload = function(){
var textbox = document.getElementById('textbox');
textbox.setSelectionRange(9, 9);
console.log(textbox.selectionStart); // 8
console.log(textbox.selectionEnd); // 8
};
</script>
支护性:ie6\7\8不支持;Chrome、Firefox、Opera、Safari都支持。测试
参考文档:https://developer.mozilla.org/en/DOM/document.activeElementspa
document.designMode = 'on';
当document.designMode = 'on'时selection的方法、selectionStart、selectionEnd在Firefox和Opera中不可使用,但Chrome和Safari能够。code