理解setEndPoint方法
理解setEndPoint方法
今天在修改控件时,使用到了setEndPoint方法,查找了MSDN,自己测试了一下,总结了该方法的使用,供大家分享。
MSDN上的解释是:
Sets the endpoint of one range based on the endpoint of another range.
表面意思是:在另一个range的endpoint基础上设置某个range的endpoint.
基本概念:
A text range has two endpoints: one at the beginning of the text range and one at the end. An endpoint can also be the position between two characters in an HTML document.
一个text range包含两个endpoints,一个在text range的开头,另一个在text range的末尾。一个endpoint也可以在HTML文档的两个字符之间。
Syntax:
TextRange.setEndPoint(sType, oTextRange)
Parameters
|
sType |
Required. String that specifies the endpoint to transfer using one of the following values.
|
||||||||
|
oTextRange |
Required. TextRange object from which the source endpoint is to be taken. |
Return Value
No return value.
举例:
//123被选中
var workRange=document.selection.createRange();
var allRange=obj.createTextRange();//obj指代文本框实例
情况一:
workRange.setEndPoint("StartToStart",allRange);
workRange.text => 0123
情况二:
workRange.setEndPoint("EndToStart",allRange);
workRange.text => 0
情况三:
workRange.setEndPoint("StartToEnd",allRange);
workRange.text => 456789
情况四:
workRange.setEndPoint("EndToEnd",allRange);
workRange.text => 123456789
解释:
情况一:
workRange.text初始值为123,两个endpoints分别位于0和1之间、3和4之间。我们简称0和1之间的endpoint为epStart,3和4之间的endpoint为epEnd,理解执行结果的核心是理解workRange.setEndPoint方法的第一个参数。同时,我们需要理解的一点就是allRange也存在两个endpoints,分别位于0之前、9之后。我们简称这两个endpoints分别为allRange_epStart和allRange_epEnd。
“StartToStart”英文解释是:Move the start of the TextRange object to the end of the specified oTextRange parameter.
在本例中,也就是指:将workRange的epStart移动到allRange的allRange_epStart,那么workRange的文本变为allRange_epStart到epEnd范围之间。AllRange_epStart成为了workRange的epStart。情况二、三、四的解释类似,就不啰唆了。
浙公网安备 33010602011771号