转载请注明: 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