现在的位置: 首页 > 综合 > 正文

JS for web developers的第十章的一段翻译

2013年08月11日 ⁄ 综合 ⁄ 共 3098字 ⁄ 字号 评论关闭

本文转载自:http://blog.sina.com.cn/s/blog_50122c300100fbqs.html

前言:由于最近在做Web html 编辑器,会用range方面知识,在查找资料的时候,发到本文,觉得对自己有帮助,特此转载。

Ranges

To allow an even greater measure of control over a page, you canuse something called a range. A range can be used to select asection of a document regardless of node boundaries (note that theselection occurs behind the scenes and
cannot be seen by theuser).
Ranges are helpful when regular DOM manipulation isn’t specificenough to change a document. And as usual, there are two differentimplementations of ranges: one from the DOM and one from InternetExplorer.

Ranges
 

为了对页面有一个更大尺度的控制,引入Ranges的感念。一个Range可以选中文档的一部分而不用考虑节点的边界(要注意的是选择发生在幕后,用户不能直接看到)。当常用的DOM操作不足够详细来改变一个文档的时候,range就有用了。一般,有两种ranges的实现:一种来自DOM,一种来自IE。

Simple selection in DOM ranges
The simplest way to select a part of the document using a range isto use either selectNode() or selectNodeContents(). These methodseach accept one argument, a DOM node, and fill a range withinformation from that node.
The selectNode() method selects the entire node, including itschildren, whereas
selectNodeContents() selects all of the node’s children.
DOM ranges中的简单选中:
 使用的range选中文档的一部分的最简单方法是使用selectNode()或者selectNodeContents()。这些方法都接受一个参数,(一个DOM节点)并且用这个节点的信息填充这个range。
 
selectNode()选中整个节点包含它的子节点,selectNodeContents()选中所有它的子节点。

Whenever you create a range, a number of properties are assigned toit:
1,startContainer — The node within which the range starts (theparent of the first node in the selection)
2,startOffset — The offset within the startContainer where therange starts. If
startContainer is a text node, comment node, or CData node, thestartOffset is the number of characters skipped before the rangestarts; otherwise, the offset is the index of the first child nodein the range.
3,endContainer — The node within which the range ends (the parentof the last node in the selection)
4,endOffset — The offset within the endContainer where the rangeends (follows the same rules as startOffset)
5,commonAncestorContainer — The first node within which bothstartContainer and
endContainer exist
These properties are all read-only and are designed to give youadditional information about the range.
  当你创建range的时候,许多属性被赋给它:
1,startContainer-range开始的节点(选中区的第一个节点的父节点)。
2,startOffset-在range开始的startContainer里面的offset。假如startContainer是一个文本节点,注释节点或者CData节点,startOffset是range开始前跳过的字母的数量;否则offset是range第一个子节点的索引。
3,endContainer-range结束的节点(选中区的最后节点的父节点)
4,endOffset-在range结束的endContainer里面的offset。(遵循startOffset规则)
5,commonAncestorContainer-startContainer和endContainer都存在其中的第一个节点。
 
这些属性都是只读的,被设计出来给你关于range详细的信息。
When you use selectNode(), the startContainer, endContainer, andcommonAncestorContainer are all equal to the parent node of thenode that was passed in; startOffset is equal to the index of thegiven node within the parent’s childNodes collection, whereasendOffset
is equal to the startOffset plus one (because only onenode is selected).
 
当你使用selectNode()时, startContainer, endContainer,和commonAncestorContainer都等于选中节点的父节点。startOffset等于选中节点在父节点中所有子节点的索引。同时endOffset等于startOffset加一(因为只用一个节点被选中)。
When you use selectNodeContents(), startContainer, endContainer,and commonAncestor Container are equal to the node that was passedin; startOffset is equal to 0; endOffset is equal to the number ofchild nodes (node.childNodes.length).
 
当你使用selectNodeContents()时,startContainer,endContainer,和commonAncestorContainer都等于被选中的节点;startOffset等于零,endOffset等于子节点的长度(node.childNodes.length)。

【上篇】
【下篇】

抱歉!评论已关闭.