理解setEndPoint方法

 

理解setEndPoint方法

今天在修改控件时,使用到了setEndPoint方法,查找了MSDN,自己测试了一下,总结了该方法的使用,供大家分享。
MSDN
上的解释是:

Sets the endpoint of one range based on the endpoint of another range.

表面意思是:在另一个rangeendpoint基础上设置某个rangeendpoint.

基本概念:

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.

StartToEnd

Move the start of the TextRange object to the end of the specified oTextRange parameter.

StartToStart

Move the start of the TextRange object to the start of the specified oTextRange parameter.

EndToStart

Move the end of the TextRange object to the start of the specified oTextRange parameter.

EndToEnd

Move the end of the TextRange object to the end of the specified oTextRange parameter.

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分别位于01之间、34之间。我们简称01之间的endpointepStart34之间的endpointepEnd,理解执行结果的核心是理解workRange.setEndPoint方法的第一个参数。同时,我们需要理解的一点就是allRange也存在两个endpoints,分别位于0之前、9之后。我们简称这两个endpoints分别为allRange_epStartallRange_epEnd

StartToStart英文解释是:Move the start of the TextRange object to the end of the specified oTextRange parameter.

在本例中,也就是指:将workRangeepStart移动到allRangeallRange_epStart,那么workRange的文本变为allRange_epStartepEnd范围之间。AllRange_epStart成为了workRangeepStart。情况二、三、四的解释类似,就不啰唆了。

posted on 2007-12-23 19:21  KaKaXu  阅读(2519)  评论(3编辑  收藏  举报

导航