轉載請注明: TheViper http://www.cnblogs.com/TheViper
以前寫過一篇更簡單的 編輯器從光標處插入圖片(失去焦點后仍然可以在原位置插入),里面只測試了ie,事實上在firefox中有個小問題,就是不管插入圖片前,編輯器有沒有焦點(光標),在插入后,光標會消失。
插入前沒有焦點

插入前有焦點

修正
function insertImage(html){ restoreSelection(); if(document.selection) currentRange.pasteHTML(html); else{ $('post_input').focus(); document.execCommand("insertImage", false,html); currentRange.collapse(); } saveSelection(); }
在execcommand前,讓編輯器獲得焦點,觸發selection,然后在后面對當前range用collapse()方法。
效果
沒有焦點

有焦點

