1. 程式人生 > >向html編輯器游標所在處插入圖片

向html編輯器游標所在處插入圖片

<textarea id="content" name="content" style="display:none"></textarea>
<iframe id="myEditor" src="Edit/editor.htm?id=contentReadCookie=1" frameborder="0" scrolling="no" width="621" height="457"></iframe>
 
<script>
function insertHTMLToEditor(imgurl){
        var myEditor = document.getElementById("myEditor").contentWindow,
        doc = myEditor.document, r = doc.selection ? doc.selection.createRange() : null;
        myEditor.focus();
        doc.execCommand("insertImage", false, "Insert_Area_Id");
        var code = doc.body.innerHTML;
        code = code.replace(/(src="Insert_Area_Id")/ig,'id="Insert_Area_Id"');
        try {doc.body.innerHTML = code}catch(e){return false}
        if(r!=null){
                try {r.move("character");r.select()}
                catch(e){}
        };
        var insertcodes = '<img src="'+imgurl+'" border="0">';
        var s = doc.body.appendChild(doc.createElement("nobr"));
        s.innerHTML = insertcodes;
        var o = doc.getElementById("Insert_Area_Id");
        o.parentNode.replaceChild(s,o);
        myEditor.focus();
}
</script>