在IE和FF中,DOM事件绑定的方法是又区别的,所以也带来了兼容性的问题,但毕竟这个问题也是比较

有共识的,所以不用担心。而且这个问题也比较容易解决,瞧:

 

 
  
  1. theFunction = function() { alert("Clicked!"); }; 
  2. theElement = document.getElementById('wikipedia'); 
  3.  
  4. // All modern browsers 
  5. if (window.addEventListener) { 
  6.     theElement.addEventListener('click', theFunction, false); 
  7.  
  8. // IE 
  9. else if (window.attachEvent) { 
  10.     theElement.attachEvent('onclick', theFunction); 
  11.  
  12. // Failure 
  13. else { 
  14.     alert("Your browser is definitely too old."); 

当然,你需要把这个处理的方法封装起来,不然每次绑定事件都要写很多重复的代码。

这里值得注意的是,在FF中,addEventListener还提供多一个capture的参数,这个capture是什么意思,

就要另起话题进行讨论了。