当iframe的designMode="On"时,在ff,如何下对iframe添加事件处理函数
这几天再搞web编辑器,就为兼容ff搞得我头都大了,特别是如何对designMode="On"的iframe添加事件。好在搞好了,happy。。。
下面是一个demo代码,说明了如何在ff下对designMode="On"的iframe添加事件
原创文章,转载请注明出处:当iframe的designMode="On"时,在ff,如何下对iframe添加事件处理函数
下面是一个demo代码,说明了如何在ff下对designMode="On"的iframe添加事件
+展开
-HTML
<html>
<head>
<title>当iframe的designMode="On"时,在ff,如何下对iframe添加事件处理函数</title>
</head>
<body>
<script>
//扩展string原型
String.prototype.trim=function()
{
return this.replace(/^\s*|\s*$/g,"");
}
//扩展string原型
String.prototype.removeHtml=function()
{
return this.replace(/<[^>]*>/gi,"");
}
alert("<aa><bb>aaa<>ddddd".removeHtml())
var IsIE=navigator.appName!="Netscape";
function SetEdit()
{
var frm=IsIE?frames["frm"]:document.getElementById('frm').contentWindow;
frm.document.designMode="On";
//绑定事件,注意要区别ie和ff的绑定,要不无法获取事件对象
if(IsIE)
{
frm.document.onkeydown=function()
{
e=frm.event;//注意要获取的是iframe中的event对象
if(e.ctrlKey&&e.keyCode==13)alert('ChildEvent');
}
}
else//而且为ff时,为了获取事件对象需要addEventListener来添加事件监听。
//而且是对iframe的contentWindow的document加事件,切记切记
{
frm.document.addEventListener("keydown",
function(e)
{
if(e.ctrlKey&&e.keyCode==13)
alert('ChildEvent');
}
,false);
}
}
</script>
<iframe src='about:blank' onload="SetEdit()" id='frm' name="frm"></iframe>
</body>
</html>
<head>
<title>当iframe的designMode="On"时,在ff,如何下对iframe添加事件处理函数</title>
</head>
<body>
<script>
//扩展string原型
String.prototype.trim=function()
{
return this.replace(/^\s*|\s*$/g,"");
}
//扩展string原型
String.prototype.removeHtml=function()
{
return this.replace(/<[^>]*>/gi,"");
}
alert("<aa><bb>aaa<>ddddd".removeHtml())
var IsIE=navigator.appName!="Netscape";
function SetEdit()
{
var frm=IsIE?frames["frm"]:document.getElementById('frm').contentWindow;
frm.document.designMode="On";
//绑定事件,注意要区别ie和ff的绑定,要不无法获取事件对象
if(IsIE)
{
frm.document.onkeydown=function()
{
e=frm.event;//注意要获取的是iframe中的event对象
if(e.ctrlKey&&e.keyCode==13)alert('ChildEvent');
}
}
else//而且为ff时,为了获取事件对象需要addEventListener来添加事件监听。
//而且是对iframe的contentWindow的document加事件,切记切记
{
frm.document.addEventListener("keydown",
function(e)
{
if(e.ctrlKey&&e.keyCode==13)
alert('ChildEvent');
}
,false);
}
}
</script>
<iframe src='about:blank' onload="SetEdit()" id='frm' name="frm"></iframe>
</body>
</html>
加支付宝好友偷能量挖...
原创文章,转载请注明出处:当iframe的designMode="On"时,在ff,如何下对iframe添加事件处理函数