var supportsRange = document.implementation.hasFeature("Range", "2.0");
var alsoSupportsRange = typeofdocument.createRange == "function";
var range = document.createRange();
<!DOCTYPE html><html><head><title>DOM Range Example</title><scripttype="text/javascript">functionuseRanges() {var range1 = document.createRange();
var range2 = document.createRange();
var p1 = document.getElementById("p1");
range1.selectNode(p1);
range2.selectNodeContents(p1);
document.getElementById("txtStartContainer1").value = range1.startContainer.tagName;
document.getElementById("txtStartOffset1").value = range1.startOffset;
document.getElementById("txtEndContainer1").value = range1.endContainer.tagName;
document.getElementById("txtEndOffset1").value = range1.endOffset;
document.getElementById("txtCommonAncestor1").value = range1.commonAncestorContainer.tagName;
document.getElementById("txtStartContainer2").value = range2.startContainer.tagName;
document.getElementById("txtStartOffset2").value = range2.startOffset;
document.getElementById("txtEndContainer2").value = range2.endContainer.tagName;
document.getElementById("txtEndOffset2").value = range2.endOffset;
document.getElementById("txtCommonAncestor2").value = range2.commonAncestorContainer.tagName;
}
</script></head><body><pid="p1"><b>Hello</b> world!</p><inputtype="button"value="Use Ranges"onclick="useRanges()" /><tableborder="0"><tr><td><fieldset><legend>range1</legend>
Start Container: <inputtype="text"id="txtStartContainer1" /><br />
Start Offset: <inputtype="text"id="txtStartOffset1" /><br />
End Container: <inputtype="text"id="txtEndContainer1" /><br />
End Offset: <inputtype="text"id="txtEndOffset1" /><br />
Common Ancestor: <inputtype="text"id="txtCommonAncestor1" /><br /></fieldset></td><td><fieldset><legend>range2</legend>
Start Container: <inputtype="text"id="txtStartContainer2" /><br />
Start Offset: <inputtype="text"id="txtStartOffset2" /><br />
End Container: <inputtype="text"id="txtEndContainer2" /><br />
End Offset: <inputtype="text"id="txtEndOffset2" /><br />
Common Ancestor: <inputtype="text"id="txtCommonAncestor2" /><br /></fieldset></td></tr></table><p><strong>Note:</strong> This example uses DOM ranges and will only work in browsers that support DOM ranges. This example will fail in Internet Explorer < 9.</p></body></html>
<!DOCTYPE html><html><head><title>DOM Range Example</title><scripttype="text/javascript">functionuseRanges() {var range1 = document.createRange();
var range2 = document.createRange();
var p1 = document.getElementById("p1");
var b1 = document.getElementById("b1");
var p2 = document.getElementById("p2");
var b2 = document.getElementById("b2");
//设置range1为b到文本节点"world!"
range1.setStartBefore(b1);
range1.setEndAfter(p1.lastChild);
range2.setStartAfter(p1.lastChild);
range2.setEndBefore(p2.firstChild);
document.getElementById("txtStartContainer1").value = range1.startContainer.id;
document.getElementById("txtStartOffset1").value = range1.startOffset;
document.getElementById("txtEndContainer1").value = range1.endContainer.id;
document.getElementById("txtEndOffset1").value = range1.endOffset;
document.getElementById("txtCommonAncestor1").value = range1.commonAncestorContainer.tagName;
document.getElementById("txtStartContainer2").value = range2.startContainer.id;
document.getElementById("txtStartOffset2").value = range2.startOffset;
document.getElementById("txtEndContainer2").value = range2.endContainer.id;
document.getElementById("txtEndOffset2").value = range2.endOffset;
document.getElementById("txtCommonAncestor2").value = range2.commonAncestorContainer.tagName;
}
</script></head><body><pid="p1"><bid="b1">Hello</b> world!</p><pid="p2"><bid="b2">Hello</b> world!</p><inputtype="button"value="Use Ranges"onclick="useRanges()" /><tableborder="0"><tr><td><fieldset><legend>range1</legend>
Start Container: <inputtype="text"id="txtStartContainer1" /><br />
Start Offset: <inputtype="text"id="txtStartOffset1" /><br />
End Container: <inputtype="text"id="txtEndContainer1" /><br />
End Offset: <inputtype="text"id="txtEndOffset1" /><br />
Common Ancestor: <inputtype="text"id="txtCommonAncestor1" /><br /></fieldset></td><td><fieldset><legend>range2</legend>
Start Container: <inputtype="text"id="txtStartContainer2" /><br />
Start Offset: <inputtype="text"id="txtStartOffset2" /><br />
End Container: <inputtype="text"id="txtEndContainer2" /><br />
End Offset: <inputtype="text"id="txtEndOffset2" /><br />
Common Ancestor: <inputtype="text"id="txtCommonAncestor2" /><br /></fieldset></td></tr></table><p><strong>Note:</strong> This example uses DOM ranges and will only work in browsers that support DOM ranges. This example will fail in Internet Explorer < 9.</p></body></html>