日期:2014-05-16 浏览次数:20463 次
<!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>