虽然我们会讲解用程序创建范围对象,但是我们把精力主要集中在如何将用户的选取范围转换成为w3c 范围或者微软的文档范围对象。
什么是范围范围是指html文档中的任意一部分内容。一个范围的开始和结束点都可以是随意的,甚至是相同的(一个空范围)。最常见的范围就是用户选取的文本。当用户在页面上选取了一部分,你就可以他的选取部分转换为范围对象。然而,你也可以让程序自动选择范围。
让我们以下面的代码为例。假设用户选择了下面的文字:
href=http://radar.oreilly.com/archives/2007/03/call_for_a_blog_1.html
class=external>call for a blogger's code of conduct
tim o'reilly calls for a blogger code of conduct. his proposals are:
take responsibility not just for your own words, but for the
comments you allow on your blog.
label your tolerance level for abusive comments.
consider eliminating anonymous comments.
你可以将用户选择转换为一个包含用户选择范围的文本的范围对象(后面讲)。根据范围对象,你能找到开始和结束的范围点。如果你愿意你可以删除它拷贝它或者用其他文本代替,甚至用html代码来代替。
这是范围对象最简单的例子了,因为他只包含文本。下面我们来看一个复杂的例子:
href=http://radar.oreilly.com/archives/2007/03/call_for_a_blog_1.html
class=external>call for a blogger's code of conduct
tim o'reilly calls for a blogger code of conduct. his proposals are:
take responsibility not just for your own words, but for the
comments you allow on your blog.
label your tolerance level for abusive comments.
consider eliminating anonymous comments.
另外一个范围对象被创建了,而且还包含html。问题在于用户的选择范围跨越了几个元素。去掉其他的内容,就剩下:
calls for a blogger code of conduct. his proposals are:
take responsibility not just for your own words, but for the
comments you allow on your blog.
label your toleran
这是一段不完整的html。幸好所有的浏览器都会转化一下:
calls for a blogger code of conduct. his proposals are:
take responsibility not just for your own words, but for the
comments you allow on your blog.
label your toleran
正如你所看到的,浏览器会添加最少的元素让这段html完整,如果你复制的话,那么这些添加的东西也会被复制。
浏览器兼容性一览在我们继续之前,有必要看看浏览器的兼容性。主要问题在于这里有不下3个范围对象的类型,你必须都有所了解才行。
module explorer 6/7
firefox 2 safari 1.3 opera 9
w3c range no yes yes yes
mozilla selection no yes incomplete yes
microsoft text range yes no no incomplete
访问用户选区要处理用户的选择就必须先访问到用户的选区。这会立马又一个代码分支:ie使用微软的方法,其他浏览器使用mozilla的方法:
复制代码 代码如下:
var userselection;
if (window.getselection) {
userselection = window.getselection();
}
else if (document.selection) { // should come last; opera!
userselection = document.selection.createrange();
}
在mozilla,safari,opera里面现在userselection是一个选择对象(selection object),在ie中是一个文本范围对象(text range object)。这个区别在后面的代码中依然有效:ie的文本范围对象和w3c的范围对象以及mozilla的选择对象有根本的不同,每一部分的代码都需要另一部分的补充。
要注意分支的顺序:mozilla selection一定要在前。因为opera两种都支持,如果你用window.getselection()读取用户的选区,opera就会创建一个选择对象,可是你用document.selection的时候他也会创建一个文本范围对象。
虽然opera支持mozilla和w3c模式很不错,但是支持ie确是有毛病,这样就不得不把window.getselection放在前面检测。
userselection的内容
现在userselection既是一个mozilla的选择对象又是ie的文本范围对象。这样他就可以使用所有的方法和属性了。
然后,mozilla的选择对象userselection里面保存的用户选择的文本(而不是html)。这样写:
复制代码 代码如下:
alert(userselection)
就会产生:
calls for a blogger code of conduct. his proposals are: take responsibilitynot just for your own words, but for the comments you allow on your blog.label your toleran如果想在微软的文本范围对象中得到相同的内容你就要使用:
var selectedtext = userselection;if (userselection.text) selectedtext = userselection.text;现在selectedtext就包含了用户选择的文本。如果你觉得这样的信息足够的话,那么就开始准备后面的工作吧。
从选择对象创建范围对象
很多时候,你想处理的是代表用户选择范围的范围对象(range object)。在微软模式中条件已经具备:userselection就是一个文本范围。在兼容w3c的浏览器中userselection依然只是一个选择对象,是时候创建一个与选择对象内容相同的范围对象了。
按照下面这样:
复制代码 代码如下:
var rangeobject = getrangeobject(userselection);
function getrangeobject(selectionobject) {
if (selectionobject.getrangeat)
return selectionobject.getrangeat(0);
else { // safari!
var range = document.createrange();
range.setstart(selectionobject.anchornode,selectionobject.anchoroffset);
range.setend(selectionobject.focusnode,selectionobject.focusoffset);
return range;
}
}
理想情况下,我们通过选择对象的getrangeat()来访问w3c范围对象。这个方法会在给定的位置返回一个范围对象:就像平常一样第一个范围对象的编号是0。(getrangeat()已经设计好如果有多处选择的情况下怎么办。在那种情况下你的代码也很简单)
自从创建一个范围不幸的是safari1.3不支持getrangeat()。因此我们需要创建一个跟用户选择一样的范围对象。这是一个很好的练习机会,可以让你知道如何创建自己的范围对象。
很明显的从创建一个对象开始:
var range = document.createrange();
现在我们已经有了一个空对象。为了把他插入到文档里面去我们需要使用setstart()函数和setend()函数。
这两个方法需要两个参数:
1、在哪个dom节点上开始或者结束的?
2、从哪个文本偏移上开始或者结束的?文本偏移就是指范围对象的第一个或者最后一个字符的位置。
让我们再来看一遍第二个例子:
href=http://radar.oreilly.com/archives/2007/03/call_for_a_blog_1.html
class=external>call for a blogger's code of conduct
tim o'reilly calls for a blogger code of conduct. his proposals are:
take responsibility not just for your own words, but for the
comments you allow on your blog.
label your tolerance level for abusive comments.
consider eliminating anonymous comments.
范围从
节点开始,并且文字偏移量是13,因为第14个字符已经是包含在范围里面的了(和通常一样,编号从0开始的)。
范围从
结束,偏移量是17,因为第18个字符时范围内的最后一个字符了。如何创建这个范围对象:
复制代码 代码如下:
var startpar = [the p node];
var endli = [the second li node];
range.setstart(startpar,13);
range.setend(endli,17);
(注意现在创建的范围对用户不可见,只在浏览器的内部)
现在我们已经创建了一个范围,我们也可以读出他的开始和结束点。startcontainer和startoffset决定了范围的开始位置,同样的endcontainer和endoffset决定了结束位置。
读取选区的开始和结束位置
不幸的是,你并不知道用户选择了页面哪个部分。所以你需要先读出用户选择的开始和结束的位置:这个必须在选择对象(selection object)里面完成,因为这时候还没有范围对象(range object)。
我们刚刚看到每一个范围对象都有标明他开始和结束位置的四个属性。选择对象也有相似的。当然是另外的名字:anchornode/anchoroffset代表开始位置,focusnode/focusoffset代表结束位置。
所以读出了选择对象的开始和结束位置之后我们就能创建范围对象了:
复制代码 代码如下:
range.setstart(selectionobject.anchornode,selectionobject.anchoroffset);
range.setend(selectionobject.focusnode,selectionobject.focusoffset);
待续
现在我们有了范围对象和微软的文本范围对象。以后我们会讲解如何使用它们,并且解决不兼容的问题。
翻译地址:http://www.quirksmode.org/dom/range_intro.html
转载请保留以下信息
作者:北玉(tw:@rehawk)
