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事件,并保存當前的焦點,再插入的時候在剛才保存的焦點處插入就可以了