<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <title>Web编辑器</title>
        <style type="text/css">body {
                font-size: 10pt;
            }
        </style>
    </head>
    <body>
        <a href='#' onclick='InsertIMG();return false;'>建立連接</a>
        <br/>
        <iframe ID="HtmlEditor" name="HtmlEditor" style="height:100px;width:300px;background:#ffffff;" frameBorder="1px" marginHeight="0" marginWidth="0" src="about:blank">
        </iframe>
        <script type="text/javascript">
            var IsIE = document.all;
            //按id获取对象 
            function $(objId, IsFrame){
                if (IsFrame) 
                    return IsIE ? window.frames[objId] : document.getElementById(objId).contentWindow;
                else 
                    return document.getElementById(objId);
            }
            
window.onload = function(){
var frm = $('HtmlEditor', true);
var doc = frm.document;
if (doc.body) {
doc.body.style.backgroundColor = '#ffffff';
doc.body.style.fontSize = '12px';
doc.body.style.fontFamily = 'verdana';
doc.body.style.margin = '4px';
doc.write('<img src="http://csdn.net/images/newcsdnlogo.gif" />');
}
doc.designMode = "On";
if (IsIE) 
doc.contentEditable = "True";
}
            function InsertIMG(){
                var frm = $('HtmlEditor', true);
                var doc = frm.document;
var txt = doc.selection.createRange().text;
if (txt) {
frm.focus();
                    doc.execCommand('createlink', false, "a.com");
// 如果選擇了圖片之類的,,那這里不執行。。
} else {
                    // 做其他事情...
}
            }
        </script>
    </body>
</html>