我在使用attachEvent时遇到了麻烦。在支持addEventListener处理程序的所有浏览器中,下面的代码都可以完美运行,但在IE中却是一场灾难。他们有自己的(不完整的)变体称为attachEvent。
现在问题来了,我如何使attachEvent以与addEventListener相同的方式工作?
这是代码:
function aFunction(idname)
{
document.writeln('<iframe id="'+idname+'"></iframe>');
var Editor = document.getElementById(idname).contentWindow.document;
/* Some other code */
if (Editor.attachEvent)
{
document.writeln('<textarea id="'+this.idname+'" name="' + this.idname + '" style="display:none">'+this.html+'</textarea>');
Editor.attachEvent("onkeyup", KeyBoardHandler);
}
else
{
document.writeln('<textarea id="hdn'+this.idname+'" name="' + this.idname + '" style="display:block">'+this.html+'</textarea>');
Editor.addEventListener("keyup", KeyBoardHandler, true);
}
}
这会调用名为 KeyBoardHandler 的函数,它看起来像这样:
function KeyBoardHandler(Event, keyEventArgs) {
if (Event.keyCode == 13) {
Event.target.ownerDocument.execCommand("inserthtml",false,'<br />');
Event.returnValue = false;
}
/* more code */
}
我不想使用任何框架,因为 A) 我正在尝试学习和理解某些东西,而 B) 任何框架都只是我不会使用的代码过载。
非常感谢您的任何帮助!