我想着在web编辑器里面插入个固定的超链接例如:点击按钮在编辑器里面直接插入 文本是:AAA 它的链接地址:XXXX.com我知道 document.execCommand('CreateLink',true,'true');//弹出一个对话框输入URL       document.execCommand('CreateLink',false,‘XXXX.com’) //需要选择文本我不想弹出对话框或者用鼠标选择文本那种形式我就想直接插入请高手指教!!!!!!!!!!!!
 

解决方案 »

  1.   

    框选改变文字链接<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    <HTML> 
    <head> 
    <script> 
    var ed = null; 
    var lightbox =popoup= null; 
    var lianjie =rang= null; 
    window.onload = function() { 
        var o = document.getElementById("ss"); 
        ed = document.all?o.contentWindow.document:o.contentDocument; 
        ed.open(); 
        ed.write(' <html> <head> <style>html,body{margin:5px;font:14px;word-wrap:break-word} </style> </head> <body id="my_body"> </body> </html>'); 
        ed.close(); 
        ed.contentEditable = true; 
        ed.designMode = 'on'; 
    ////////////////////////////////////////////////////// 
        lightbox = document.getElementById('lb'); 
    lightbox.style.width = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth) + "px"; 
    lightbox.style.height = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight) + "px" 
    lightbox.style.display = "none"; 
    ///////////////////////////////////////////////////// 
    popoup = document.getElementById('popoup'); 
    popoup.style.left = (document.documentElement.scrollWidth - popoup.offsetWidth)/2+"px"; 
    popoup.style.top = (document.documentElement.scrollHeight - popoup.offsetHeight)/2+"px"; 
    popoup.style.display = "none"; 
    }; function ss(){ 
        if(document.all)rang=ed.selection.createRange(); 
    popoup.style.display =lightbox.style.display = ""; 
    document.getElementById('ll').value=""; 
    } function linka(){ 
        lianjie = document.getElementById('ll').value; 
    popoup.style.display =lightbox.style.display = "none"; if(document.all){rang.execCommand("CreateLink",false,lianjie)} 
    else 
    ed.execCommand("CreateLink",false,lianjie); //rang.parentElement().setAttribute("target","_blank");
    //rang.parentElement().outerHTML=rang.parentElement().outerHTML.replace("<A","<A   target=\"_blank\"");    } 
    </script> 
    </head> 
    <BODY> 
    <div id='lb' style="filter:alpha(opacity=50);Opacity:0.5; background-color:#CCCCCC; position:absolute; left:0px; top:0px;"> </div> 
    <div id='popoup' style="border:1px solid #666666 ; padding:50px; position:absolute; background-color:#FFFFFF"> 
    输入连接: <input id='ll' style=" width:250px;" type="text" /> <br> 
    <input value="确定" type="button" onclick="linka()" /> 
    </div> 
    <IFRAME id="ss" height="200" src="about:blank" width="200" > </IFRAME> 
    <br> <br> <input value='link' type="button" onclick="ss()" /> 
    <input value='innerHTML' type="button" onclick="alert(ed.body.innerHTML)" > 
    </BODY> </HTML>
      

  2.   

    我可能没有说明白我是这么理解的可能要分3部分第一:插入显示的文本第二:把插入的文本选中第三:让选中的文本 'CreateLink' 地址:xxx.com