日期:2014-05-16  浏览次数:20463 次

range对象的surroundContents方法怎么用?
JScript code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
  //<![CDATA[
  window.onload = function() {
    /*
     * startContainer
     * startOffset
     * endContainer
     * endOffset
     * commonAncestorContainer
     *
     * selectNode(selNode);
     * selectNodeContents(selNode);
     * setStartBefore(refNode);
     * setStartAfter(refNode);
     * setEndBefore(refNode);
     * setEndAfter(refNode);
     *
     * setStart(node, offset);
     * setEnd(node, offset);
     *
     * deleteContents();
     * extractContents();
     *
     * insertNode(cteNode);
     * surroundContents(cteNode);
     */

    var p1 = document.getElementById("p1");
    var helloNode = p1.firstChild.firstChild;
    var worldNode = p1.lastChild;

    var range = document.createRange();
    range.setStart(helloNode, 2);
    range.setEnd(worldNode, 3);

    var span = document.createElement("span");
    span.style.color = "yellow";

    alert(1);

    range.surroundContents(f);  //这里出错;

    alert(2);
  };
  //]]>
</script>
</head>

<body>
<p id="p1"><b>Hello</b> world!</p>
</body>
</html>



上边是我的代码,按照书上的例子在ff中测试的,不知道什么导致的错误。请高手指点!

------解决方案--------------------
好像是说边界不符合要求, 不能夸标签的吧
The boundary-points of a range does not meet specific requirements.

[code=JScript]

[/codevar p1 = document.getElementById("p1");
var helloNode = p1.firstChild.firstChild;
var worldNode = p1.lastChild;

var range = document.createRange();
range.setStart(helloNode, 0);
range.setEnd(helloNode, 5);

var span = document.createElement("span");
span.style.color = "yellow";

alert(1);

range.surroundContents(span); //这里出错;

alert(2);]