试图理解javascript';关键事件处理

试图理解javascript';关键事件处理,javascript,Javascript,我想知道为什么的源代码使用所有这些if-else来添加事件侦听器: if (document.addEventListener) { document.addEventListener("keydown",keydown,false); document.addEventListener("keypress",keypress,false); document.addEventListener("keyup",keyup,false); document.addEvent

我想知道为什么的源代码使用所有这些if-else来添加事件侦听器:

if (document.addEventListener)
{
   document.addEventListener("keydown",keydown,false);
   document.addEventListener("keypress",keypress,false);
   document.addEventListener("keyup",keyup,false);
   document.addEventListener("textInput",textinput,false);
}
else if (document.attachEvent)
{
   document.attachEvent("onkeydown", keydown);
   document.attachEvent("onkeypress", keypress);
   document.attachEvent("onkeyup", keyup);
   document.attachEvent("ontextInput", textinput);
}
else
{
   document.onkeydown= keydown;
   document.onkeypress= keypress;
   document.onkeyup= keyup;
   document.ontextinput= textinput;   // probably doesn't work
}

是否与大多数浏览器兼容?

此评论与您的照片完全一致。。XD谢谢!谢谢,我认为这是一种恭维
if (document.addEventListener) // DOM spec that all browsers should follow

else if (document.attachEvent) // but unfortunately IE does not follow them    

else // and there may be other older browsers that follow neither