当前位置:编程学习 > JS >>

关于js 用iframe做的在线编辑器如何保存输入焦点的问题

大意如题:要求支持IE和FF。。最好能给下代码

追问:此代码应该不支持FF。如果以你这样给iframe注册事件 。在FF中是不能的。

答案: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事件,并保存当前的焦点,再插入的时候在刚才保存的焦点处插入就可以了

html页面中有
<iframe id="edi" ></iframe>
<input id="text1" type="text" />
<input id="ok" type="button" value="确定" />

利用id名.focus实现!

//自动给表单上的文本框加上动态效果Js

//=================================
var __txtArray;
function initAutoFormStyle()
{
var inputArry;
inputArry = document.getElementsByTagName("input");
textArry = document.getElementsByTagName("textarea");
for(var i=0;i<inputArry.length+textArry.length;i++)
{
var tmpInput = null;
if(i< inputArry.length)
{
tmpInput = inputArry[i];
if(tmpInput.type == 'text' || tmpInput.type == 'password' || tmpInput.type == 'file')
{
setEvent(tmpInput);
}
}
else
{
tmpInput = textArry[inputArry.length - i];
setEvent(tmpInput);
}
}
}

function setEvent(obj)
{
var newClassName = 'text';
var focusClassName = 'focus';
if(obj.style.className != undefined)
{
newClassName = obj.style.className + ' ' + newClassName;
focusClassName = obj.style.className + ' ' + focusClassName;
}
obj.className = newClassName;
obj.onblur = function(){
this.className = newClassName;
}
obj.onfocus =function(){
this.className = focusClassName;
}
}

上一个:求高手帮忙在代码里加个JS调用。高分送上!
下一个:离开页面时检测表单元素是否被修改,提示保存的js代码

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,