轉載請註明: TheViper http://www.cnblogs.com/TheViper html
之前寫過一篇更簡單的 編輯器從光標處插入圖片(失去焦點後仍然能夠在原位置插入),裏面只測試了ie,事實上在firefox中有個小問題,就是無論插入圖片前,編輯器有沒有焦點(光標),在插入後,光標會消失。編輯器
插入前沒有焦點post
插入前有焦點測試
修正url
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()方法。spa
效果firefox
沒有焦點rest
有焦點code
下載htm