javascript用iframe做的在线编辑器如何保存输入焦点的问题
var myeditorid = "";
//是否开启设计模式
function editordesignmode(editorid)
{
var deditor = document.getelementbyid(editorid+"_iframe").contentwindow;
deditor.document.designmode="on";
//deditor.contenteditable="true";
deditor.document.open();
deditor.document.writeln("<html><head><title></title></head><body></body></html>");
deditor.document.close();
myeditorid = editorid+"_iframe";
deditor.document.body.onclick =getpos;
deditor.document.body.onselect =getpos;
deditor.document.body.onkeyup =getpos;
}
function getpos()
{
var deditor = document.getelementbyid(myeditorid).contentwindow;
deditor.pos = deditor.document.selection.createrange();
//alert("dafd");
}
//插入图片至编辑器
function insertsimagego(imageurl,editorid)
{
var deditor = document.getelementbyid(editorid+"_iframe").contentwindow;
// 聚焦编辑器
deditor.focus();
//插入图片处理
if(imageurl != "")
{
if (window.navigator.useragent.indexof("msie")<1)
{
deditor.document.execcommand('insertimage',false,imageurl);
}
else
{
var _image = document.createelement("img");
_image.src=imageurl;
_image.border="0";
if (deditor.document.selection.type.tolowercase() != "none")
{
deditor.document.selection.clear() ;
}
deditor.pos.pastehtml(_image.outerhtml);
}
}
// 聚焦编辑器
deditor.focus();
hidediv("popdiv");
}
在iframe里加上onclick,onselect,onkeyup事件,并保存当前的焦点,再插入的时候在刚才保存的焦点处插入就可以了