现在的位置: 网页制作教程网站制作教程 >正文
JavaScript脚本

Javascript中Selection操作方法

发表于2017/3/15 网站制作教程 0条评论 ⁄ 热度 2,014℃

Javascript中Selection简介

selection是对当前激活选中区(即高亮文本)进行操作。

在非IE浏览器(Firefox、Safari、Chrome、Opera)下可以使用window.getSelection()获得selection对象,本文讲述的是标准的selection操作方法。

Javascript中Selection术语

以下几个名词是英文文档中的几个名词。

anchor

选中区域的“起点”。

focus

选中区域的“结束点”。

range

是一种fragment(HTML片断),它包含了节点或文本节点的一部分。一般情况下,同一时刻页面中只可能有一个range,也有可能是多个range(使用Ctrl健进行多选,不过有的浏览器不允许,例如Chrome)。可以从selection中获得range对象,也可以使用document.createRange()方法获得。

Javascript中Selection属性

anchorNode

返回包含“起点”的节点。

anchorOffset

“起点”在anchorNode中的偏移量。

focusNode

返回包含“结束点”的节点。

focusOffset

“结束点”在focusNode中的偏移量。

isCollapsed

“起点”和“结束点”是否重合。

rangeCount

返回selection中包含的range对象的数目,一般存在一个range,Ctrl健配合使用可以有多个。

Javascript中Selection方法

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都支持。

document.designMode = 'on';

当document.designMode = 'on'时selection的方法、selectionStart、selectionEnd在Firefox和Opera中不可以使用,但Chrome和Safari可以。

  • 暂无评论