with(document.selection.createRange())
{
collapse();
select();
} 哪位好心人给解释一下

解决方案 »

  1.   

    with 语句用于设置代码在特定对象中的作用域。它的语法:with (expression) statement例如:var sMessage = "hello";
    with(sMessage) {
      alert(toUpperCase()); //输出 "HELLO"
    }
    在这个例子中,with 语句用于字符串,所以在调用 toUpperCase() 方法时,解释程序将检查该方法是否是本地函数。如果不是,它将检查伪对象 sMessage,看它是否为该对象的方法。然后,alert 输出 "HELLO",因为解释程序找到了字符串 "hello" 的 toUpperCase() 方法。又例如:
    x = Math.cos(3 * Math.PI) + Math.sin(Math.LN10) 
    y = Math.tan(14 * Math.E) 
    当使用 with 语句时,代码变得更短且更易读: with (Math){ 
       x = cos(3 * PI) + sin (LN10)  
       y = tan(14 * E) 
    } 不知道你能否明白!
      

  2.   

    with 语句
    为语句设定默认对象。
    with (object)
       statements 
    参数
    object
    新的默认对象。
    statements
    一个或多个语句,object 是该语句的默认对象。
    说明
    with 语句通常用来缩短特定情形下必须写的代码量。在下面的例子中,请注意 Math 的重复使用:
    x = Math.cos(3 * Math.PI) + Math.sin(Math.LN10) y = Math.tan(14 * Math.E)
    当使用 with 语句时,代码变得更短且更易读:
    with (Math){   x = cos(3 * PI) + sin (LN10)    y = tan(14 * E)}再如:function test(){
    var o={
         name:"name",
         sex: "man"
         }
      with(o){
         //这里可以直接使用o对象。
        alert(name+";"+sex);
      }
    }
    //显示 name;man
      

  3.   

    w3c标准中的selection对象和range对象都有 collapse()方法。功能相似,但还是有区别的。Selection对象的collapse()方法:
    collaspeCollapses the current selection to a single point. The document is not modified. If the current is focused and editable, the caret will blink there.sel.collapse(parentNode,offset);parentNode    The caret location will be within thid node.offset    0 - The caret location will be within thid node.    1 - Collapses the selection form the anchor to the end of parentNode's text.var hint=docuemnt.getElementById("hint");
    hint.focus();
    window.getSelection().collapse(hint,0);注意光标移动到 hint 元素的开始或结尾,此时光标仍然位于 hint 元素的内部。 Range对象的collapse()方法
    collapseCollapses the Range to one of its boundary points.range.collapse(toStart);toStart: A boolean, true collapses the Range to its start, false to its end.A collapsed Range is empty, containing no content, spcifying a single-point in a DOM tree.
    range=document.createRange();
    referenceNode=document.getElementsByTagName("div").item(0);
    range.selectNode(referenceNode);
    range.collapse(true);光标移动到边界,此时光标位于elem元素的外部。 下面这段示例代码演示在当前光标处插入h3元素,并将光标移动到新插入的h3元素上。
    //获取光标处的DOM对象
    var node=this.searchBlockElement();//当前光标处插入h3对象
    var elem=document.createElement("h3");
    elem.style.cssText="padding:6px 0;font-size:14px;";
    node.appendChild(elem);//将光标移动到h3元素
    if(userAgent.ie){//IE
        //创建TextRange对象
        var range=this.editor.document.body.createTextRange();
        //移动文本范围以便范围的开始和结束位置能够完全包含给定元素(obj)的文本(vapour)
        range.moveToElementText(elem);
        //光标移动到文本范围的结尾
        range.collapse(false);
        //将当前选中区设置为当前对象
        range.select();    
    }else{//other
        var selection=this.editor.getSelection();
        selection.collapse(elem,1);
    }获取光标处的父对象请参考上一篇 富文本编辑器--获取光标当前位置处的父对象 。
    null